弦乐! [英] Strings!

查看:44
本文介绍了弦乐!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我是C语言的新手(但不是结构化编程),我有一些关于字符串的问题:

1)这两种字符串访问方法有哪些更快?

- 直接访问char数组的元素(myarray [i])

- 通过指向char(* mypointer)的指针访问


2)在堆中或在
$中分配字符串更有效(时间) b $ b堆栈?


3)为字符串赋值的正确方法是什么?也许

使用ols字符串中新值的strcpy?


4)存在一个返回子串的函数(从start元素到a

完成元素的字符串?


5)还有一些其他(非ANSI标准)C库已知

非常有用并经常用于实际编程?一些网站链接

这个?


提前谢谢


-

Il Prof.

Hi!

I''m new in C language (but not in structured programming) and i have
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray[i])
- Access through a pointer to char (*mypointer)

2) Is more (time) efficient the allocation of strings in the heap or in
the stack?

3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?

4) Exists a function that return a substring (from a start element to a
finish element) of a string?

5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?

Thank you in advance

--
Il Prof.

推荐答案

Il Prof< ch ********* @ chiedisulng.org>写道:
Il Prof <ch*********@chiedisulng.org> wrote:
我是C语言的新手(但不是结构化编程)我有一些关于字符串的问题:
1)这两个字符串访问方法是什么更快?
- 直接访问char数组的元素(myarray [i])
- 通过指向char(* mypointer)的指针访问


像myarray [i]这样的访问只是一种奇特的写作方式*(myarray + i)

(这就是编译器要将它翻译成的),所以

不应该有太大区别。此外,C标准不是b $ b b b任何关于任何操作速度的陈述,所以

问题可以,如果有的话,只能回答一定的实施/>
心理。但是有了这样的问题,你会在这里偏离主题

因为那不是关于C的问题,而是一些实现它。

2)更多(时间) )有效地在堆中或堆栈中分配字符串?


首先,术语堆栈都不是。或堆完全按照(C89)标准在

中提到,所以开始时根本就没有答案。

但是根据事情通常如何实施你可以' '$

比较堆上的分配(这是你通常在调用malloc()的时候执行的
)和堆栈上的分配(当你定义
一个函数中的数组) - 在第一种情况下,你进行动态的

分配,在第二种情况下你分配一个固定长度的数组

(除非你有一个C99兼容的编译器,其中引入了可变长度

数组 - 但你仍然只能得到一个字符串

你可以在当前函数中使用,而不是用malloc()分配的字符串

,也可以在其他任何地方使用。

但是在任何情况下唯一可能的答案是它取决于你正在使用的

实现以及你使用它的硬件

with。

3)为字符串赋值的正确方法是什么?也许
使用ols字符串中新值的strcpy?


如果你想要复制一个字符串,那么strcpy()和strncpy()将是通常的嫌疑人



4 )是否存在一个返回字符串子字符串(从起始元素到
结束元素)的函数?


现在,这取决于你想从函数返回什么。如果它是指向该子字符串的
a指针(但不是一个真正的新字符串,即只是一个指向原始字符串的
指针)而不是返回

指向旧字符串开头的指针与子字符串相对于原始字符串开头的偏移量的总和。如果你想要

来返回一个新字符串,那么你必须为子字符串分配内存

并将你想要的原始字符串部分复制到新字符串中。 />
没有标准的功能可以完成所有这些 - 但是可能会有

图书馆这样的功能。

5)有一些其他(非ANSI标准)C库已知非常有用并且经常用于实际编程中?一些网页链接
这个?
I''m new in C language (but not in structured programming) and i have
some questions about strings: 1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray[i])
- Access through a pointer to char (*mypointer)
An access like myarray[i] is just a fancy way of writing *(myarray+i)
(that''s what the compiler is going to translate it to), so there
shouldn''t be much of a difference. Further, the C standard isn''t
making any statements about the speed of any operations, so the
question could, if at all, only be answered for a certain imple-
mentation. But with questions like that you would be off-topic here
since that''s not a question about C but some implementation of it.
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?
First of all, neither the term "stack" or "heap" are mentioned in
the (C89) standard at all, so there''s really no answer to begin with.
But arguing from how things normally get implemented you can''t
compare allocation on the heap (that''s what you typically do when
you call malloc()) and allocation on the stack (when you define
an array in a function) - in the first case you do dynamical
allocation and in the second you "allocate" a fixed length array
(unless you have a C99-compliant compiler where variable length
arrays have been introduced - but you still will get only a string
that you can use in the current function, in contrast to a string
allocated with malloc(), which can be used also everywhere else).
But in any case the only possible answer is that it depends on the
implementation you''re using as well as the hardware you''re using it
with.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?
If you want to copy a string then strcpy() and strncpy() would be
the usual suspects.
4) Exists a function that return a substring (from a start element to a
finish element) of a string?
Now, this depends on what you want to return from the function. If it''s
a pointer to that substring (but not a real new string, i.e. just a
pointer pointing within the original string) than you would just return
the sum of the pointer to the start of the old string and the offset of
the substring, relative to the start of the original string. If you want
to return a new string then you have to allocate memory for the substring
and copy the part of the original string you want into the new string.
There''s no standard function that does all that - but there are probably
dozends of libraries with functions like that.
5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?




因为你可能有成千上万的非常有用的非ANSI

库指定你感兴趣的是什么样的功能。但是由于非ANSI库通常是平台,所以你可能会更好地询问有关团队

与您的平台。

问候,Jens

-

\ Jens Thoms Toerring ___ < a href =mailto:Je *********** @ physik.fu-berlin.de> Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de




" Il Prof" < CH ********* @ chiedisulng.org>写了

"Il Prof" <ch*********@chiedisulng.org> wrote

我是C语言的新手(但不是结构化编程),我有一些关于字符串的问题:

1)这两种字符串访问方法的速度更快?
- 直接访问char数组的元素(myarray [i])
- 通过指向char(* mypointer)的指针访问

非常不可能有所不同。在过去,指针方法通常更快(因为数组需要一个地址计算,但是现在的硬件将在同一条指令中执行它)。
2)堆中或堆栈中字符串的分配是否(时间)更高效?

堆栈。堆需要你调用malloc()和free(),这些都是非平凡的,虽然不是很慢。堆栈通常只需要从堆栈指针中减去

3)为字符串赋值的正确方法是什么?也许
使用ols字符串中新值的strcpy?

char str [64];


strcpy(str,"这是一个字符串);
4)存在一个函数,它返回一个字符串的子字符串(从一个起始元素到一个
结束元素)?

No.你可以写一个(你可以返回一个用

malloc分配的字符串,或传入一个缓冲区)。一些函数,比如strstr和strchr在传递的字符串中返回

指针,但没有标准的库函数返回

a子字符串。
5)还有一些其他的(非ANSI标准)已知的C库非常有用并且经常用于实际编程中?一些网站链接
这个?

I''m new in C language (but not in structured programming) and i have
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray[i])
- Access through a pointer to char (*mypointer)
Very unlikely to be a difference. In the olden days the pointer method was
usually faster (because the array required an address calculation, but
modern hardware will do it in the same instruction).
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?
The stack. The heap requires you to call malloc() and free() which are
non-trivial, though not slow. The stack normally just requires a subtraction
from the stack pointer.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?
char str[64];

strcpy(str, "This is a string");
4) Exists a function that return a substring (from a start element to a
finish element) of a string?
No. You could write one (you could either return a string allocated with
malloc, or pass in a buffer). Some functions, like strstr and strchr return
pointers within the passed string, but no standard library function returns
a sub string.
5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?



这将产生一场火焰战争。我会说不。问题是字符串

处理是一个基本的操作,如果你使用第三方库

你在所有代码中创建一个依赖。这通常是一件糟糕的事情。因此,最好坚持使用stdlib函数进行字符串处理。

但是你可以看看微软安全。字符串库,如果你想要沿着那条路走下去,我认为Jacob Navia也有一个图书馆,他很高兴能够给出。


This will create a flame war. I would say no. The problem is that string
handling is such a basic operation, that if you use a thrid party library
you create a dependency throughout all your code. This is generally a bad
thing. So it is best to stick to stdlib functions for your string handling.
However you could look at say the Microsoft "safe" string library, if you
want to go down that route, and I think Jacob Navia also has a library he is
happy to give out.


>我是C语言的新手(但不是结构化编程),我有
>I''m new in C language (but not in structured programming) and i have
关于字符串的一些问题:

1)这两个字符串访问方法中的哪一个更快?
- 直接访问char数组的元素(myarray [i])
- 通过指向char(* mypointer)的指针访问


ANSI C并不保证比其他任何东西更快或更慢的价格(或特别是

,如果你没有指定硬件,操作系统,编译器,编译器版本,

编译器序列号等。)

2)更多(时间)有效的字符串分配堆栈还是堆栈?


否。这两个地方都不存在。

一般来说,分配自动变量比调用

malloc更快( )。无保证。

3)为字符串赋值的正确方法是什么?也许
使用ols字符串中新值的strcpy?


strcpy(),strncpy()和sprintf()常用,

很多其他可能性。

4)存在一个函数,它返回一个字符串的子字符串(从start元素到
finish元素)?


如果字符串的结尾和子字符串的结尾不在同一个地方的

,则必须复制或修改原始字符串。


长度为n的字符串中的字符串位置从0到n-1编号。

复制位置i到位置j,包括字符串

另一个字符串:


strncpy(new_buffer,old_buffer + i,j-i + 1);

new_buffer [j -i + 1] =''\ 0'';


但是,如果j< i,或i或j引用

位于原始字符串之外的位置。

5)还有一些其他(非ANSI标准)C库已知
非常有用并经常用于实际编程?一些网页链接
这个?
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray[i])
- Access through a pointer to char (*mypointer)
ANSI C does not guarantee that aything is faster than or slower
than or about the same speed as anything else (especially
if you do not specify hardware, OS, compiler, compiler version,
compiler serial number, etc.).
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?
No. Neither place is guaranteed to exist.
In general, allocation of auto variables is faster than calling
malloc(). No guarantees.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?
strcpy(), strncpy(), and sprintf() are commonly used, among
lots of other possibilities.
4) Exists a function that return a substring (from a start element to a
finish element) of a string?
If the end of the string and the end of the substring are not in
the same place, you will have to copy or modify the original string.

String positions in a string of length n are numbered from 0 to n-1.
To copy position i thru position j, inclusive, of a string to
another string:

strncpy(new_buffer, old_buffer + i, j-i+1);
new_buffer[j-i+1] = ''\0'';

however, this may misbehave badly if j < i, or either i or j references
positions outside the original string.
5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?




MySQL客户端库。请参阅 http://www.mysql.org/

Gordon L. Burditt



The MySQL client library. See http://www.mysql.org/ .

Gordon L. Burditt


这篇关于弦乐!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆