Pointers on C——9 Strings, Characters, and Bytes.2

9.2 String Length

The length of a string is the number of characters it contains. The length is easily computed by counting the characters, as is done in Program 9.1. This implementation illustrates the type of processing used when dealing with strings, but in fact you rarely need to write string functions because the ones provided in the standard library usually will do the job. Should you wish to write a string function, though, be aware that the Standard reserves all function names that begin with str for future expansion of the library.

字符串的长度就是它所包含的字符个数。我们很容易通过对字符进行计数来计算字符串的长度,程序9 .1就是这样做的。这种实现方法说明了处理字符串所使用的处理过程的类型。但是,事实上你极少需要编写字符串函数,因为标准库所提供的函数通常能完成这些任务。不过,如果你还是希望自己编写一个字符串函数,请注意标准保留了所有以str 开头的函数名,用于标准库将来的扩展。


The prototype for the library strlen is:

库函数strlen 的原型如下:


size_t strlen( char const *string );


Note that strlen returns a value of type size_t. This type is defined in the include file stddef.h and is an unsigned integer type. Using unsigned values in expressions can lead to unexpected results. For example, the following two statements appear to be equivalent,

注意strlen 返回一个类型为size t 的值。这个类型是在头文件stddef.h 中定义的,它是一个无符号整数类型。在表达式中使用元符号数可能导致不可预料的结果。例如,下面两个表达式看上去是相等的:


if( strlen( x ) >= strlen( y ) ) ...

if( strlen( x ) – strlen( y ) >= 0 ) ...


but they are not. The first works as you would expect, but the second one is always true. The result of strlen is unsigned, so the expression on the left of the >= is unsigned, and unsigned values can never be negative.

但事实上它们是不相等的。第1 条语句将按照你预想的那样工作,但第2 条语句的结果将永远是真。strlen 的结果是个无符号数,所以操作符>=左边的表达式也将是无符号数,而无符号数绝不可能是负的。


/*

** Compute the length of the string argument.

*/

#include <stddef.h>

size_t

strlen( char const *string )

{

int length;

for( length = 0; *string++ != '\0'; )

length += 1;

return length;

}

Program 9.1 String length


Expressions containing both signed and unsigned values can also produce strange results. The following statements are not equivalent for the same reason as the previous pair.

表达式中如果同时包含了有符号数和无符号数,可能会产生奇怪的结果。和前一对语句一样,下面两条语句并不相等,其原因相同。


if( strlen( x ) >= 10 ) ...

if( strlen( x ) - 10 >= 0 ) ...


Casting the value returned by strlen to an int eliminates this problem.

如果把strlen 的返回值强制转换为int ,就可以消除这个问题。


It is tempting to write your own strlen function, making judicious use of register declarations and clever tricks to make it faster than the library function. It rarely works. The standard library functions are sometimes implemented in assembly language in order to exploit the speed of special string manipulation instructions provided by certain machines. Even on machines without such instructions, your time is better spent concentrating on the algorithms for other parts of your program.Finding a better algorithm is more effective than tuning a bad one, and it is more efficient to reuse existing software than to reinvent it.

你很可能想自行编写strlen 函数,灵活运用register 声明和一些聪明的技巧使它比库函数版本效率更高。这的确是个诱惑,但事实上很少能够如愿。标准库函数有时是用汇编语言实现的,目的就是为了充分利用某些机器所提供的特殊的字符串操纵指令,从而追求最大限度的速度。即使在没有这类特殊指令的机器上,你最好还是把更多的时间花在程序其他部分的算法改进上。寻找一种更好的算法比改良一种差劲的算法是有效率,复用已经存在的软件比重新开发一个效率更高。


上一章 Pointers on C——9  Strings, Characters, and Bytes.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值