由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 设计一个string class,是应该用linked list还是array?
相关主题
在C++里处理string用什么比较好?请教一个简单的java问题
C++ vs Java能否创立一个functional programming的版面
问几个C++的题java如何保护传入的参数不被函数改动?
也谈OOP跟FP之争从今天开始起,学C++!
go也是三种paradigm混合的语言怎么这里这么多人学python
有什么web server 可以后台接actorHaskell很难学。。
FP更接近人的思维粉FP的人是因为把电脑想象成图灵机了
没人觉得python的string是immutable不爽吗?functional programming?
相关话题的讨论汇总
话题: string话题: linked话题: list话题: array话题: 字符串
进入Programming版参与讨论
1 (共1页)
h*****n
发帖数: 209
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: hanuman (神猴), 信区: JobHunting
标 题: 设计一个string class,是应该用linked list还是array?
发信站: BBS 未名空间站 (Sun Oct 3 23:03:21 2010, 美东)
有两种方案,一种是用array来存放字符串,另一种是用linked list来存放字符串。
用array的话,访问string里面的某个字符会很快,但是执行两个字符串相加操作的时
候会比较慢。
用linked list的话,它访问string的某个字符比较慢,但执行字符串相加操作会比较
快。
那这个string class到底如何设计比较好呢?
g*****g
发帖数: 34805
2
Of course array, and + operation is not faster with linked list,
as you have to duplicate the storage.
If A = B + C, A shouldn't share any storage with B or C

【在 h*****n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: hanuman (神猴), 信区: JobHunting
: 标 题: 设计一个string class,是应该用linked list还是array?
: 发信站: BBS 未名空间站 (Sun Oct 3 23:03:21 2010, 美东)
: 有两种方案,一种是用array来存放字符串,另一种是用linked list来存放字符串。
: 用array的话,访问string里面的某个字符会很快,但是执行两个字符串相加操作的时
: 候会比较慢。
: 用linked list的话,它访问string的某个字符比较慢,但执行字符串相加操作会比较
: 快。
: 那这个string class到底如何设计比较好呢?

s******e
发帖数: 493
3
It really depends on how often you want to do those operations and which
language.
If C/C++, or maybe any languages where array must be in a consective memory
space, as you said linked list will be faster on "+" operation. but random
access will be slow. I will vote for linked list if you must choose from two.
Or you can think from a different direction, like java, create a string
constant pool, and make string objects immutable.
w****i
发帖数: 964
4
Using linked list to stored string is really an unusual practice.
a bit shocked to see this idea.
g*****g
发帖数: 34805
5
As I say, you can't share storage between 2 string instances
unless it's the same immutable string.
If you want to use a linked list, you have to duplicate the
linked list, which negate all the benefits of faster concatenation.
And if you don't, let's say String a = b + c, now you make a change
to c, a is changed too, probably not what you want.

memory
two.

【在 s******e 的大作中提到】
: It really depends on how often you want to do those operations and which
: language.
: If C/C++, or maybe any languages where array must be in a consective memory
: space, as you said linked list will be faster on "+" operation. but random
: access will be slow. I will vote for linked list if you must choose from two.
: Or you can think from a different direction, like java, create a string
: constant pool, and make string objects immutable.

t****u
发帖数: 8614
6
为啥要re-invent the wheel?
直接用std::string不行吗?
如果需要一些其它功能,做个wrapper class,弄个std::string的member不就行了。

【在 h*****n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: hanuman (神猴), 信区: JobHunting
: 标 题: 设计一个string class,是应该用linked list还是array?
: 发信站: BBS 未名空间站 (Sun Oct 3 23:03:21 2010, 美东)
: 有两种方案,一种是用array来存放字符串,另一种是用linked list来存放字符串。
: 用array的话,访问string里面的某个字符会很快,但是执行两个字符串相加操作的时
: 候会比较慢。
: 用linked list的话,它访问string的某个字符比较慢,但执行字符串相加操作会比较
: 快。
: 那这个string class到底如何设计比较好呢?

t****u
发帖数: 8614
7
这个东西,就算要reinvent the wheel。
最简单的方法就是refer to std::string,那边是array的。
如果linked list好,早就一大堆library用linked list来实现string了。
这些都是前人的智慧总结。

【在 g*****g 的大作中提到】
: As I say, you can't share storage between 2 string instances
: unless it's the same immutable string.
: If you want to use a linked list, you have to duplicate the
: linked list, which negate all the benefits of faster concatenation.
: And if you don't, let's say String a = b + c, now you make a change
: to c, a is changed too, probably not what you want.
:
: memory
: two.

n******n
发帖数: 12088
8
托老也来这里?不是都转型了么?呵呵。
肯定是面试题。

【在 t****u 的大作中提到】
: 为啥要re-invent the wheel?
: 直接用std::string不行吗?
: 如果需要一些其它功能,做个wrapper class,弄个std::string的member不就行了。

d*****u
发帖数: 43
9
一个字节的字符,耗用一个或两个指针,这样的字符串要用多少内存?
谁要用这样的字符串?
1 (共1页)
进入Programming版参与讨论
相关主题
functional programming?go也是三种paradigm混合的语言
Python Q: function pass in struct pointer, come back with data filled有什么web server 可以后台接actor
学FP不是为了写代码, 而是为了优秀的架构.FP更接近人的思维
JS就应该杜绝OOP没人觉得python的string是immutable不爽吗?
在C++里处理string用什么比较好?请教一个简单的java问题
C++ vs Java能否创立一个functional programming的版面
问几个C++的题java如何保护传入的参数不被函数改动?
也谈OOP跟FP之争从今天开始起,学C++!
相关话题的讨论汇总
话题: string话题: linked话题: list话题: array话题: 字符串