string.h和strings.h的区别

strings.h头文件是从BSD系UNIX系统继承而来,里面定义了一些字符串函数,如bzero等。这些函数曾经是posix标准的一部分,但是在POSIX.1-2001标准里面,这些函数被标记为了遗留函数而不推荐使用。在POSIX.1-2008标准里已经没有这些函数了。如下:

int bcmp(const void *, const void *, size_t); /* 用memcmp替代 */
void bcopy(const void *, void *, size_t); /* 用memcpy, memmove替代 */
void bzero(void *, size_t); /* 用memset替代 */
int ffs(int); /* string.h 中有 */
char *index(const char *, int); /* 用strchr替代 */
char *rindex(const char *, int); /* 用strrchr替代 */
int strcasecmp(const char *, const char *); /* string.h 中有 */
int strncasecmp(const char *, const char *, size_t); /* string.h 中有 */

这两个头文件都在linux的/usr/include目录下面,后者比前者多了一个s,一般使用以string.h(没有s)的为主,那strings.h(有s)什么时候使用呢?打开这个头文件,可以看见区别如下:
/* We don’t need and should not read this file if <string.h> was already
read. The one exception being that if __USE_BSD isn’t defined, then
these aren’t defined in string.h, so we need to define them here. */
所以,一般使用前者就可以了

### Differences Between `string.h` and `std::string` In C programming, handling strings can be done using two primary methods: through functions provided by the standard library header `<string.h>` or via the C++ Standard Library class `std::string`. #### Functions Provided by `<string.h>` The `<string.h>` header offers a set of low-level operations for manipulating arrays of characters (null-terminated byte strings). These include: - **Copying Strings**: The function `strcpy(dest, src)` copies the string pointed to by `src`, including the terminating null character, into the array pointed to by `dest`. - **Concatenating Strings**: Using `strcat(s1, s2)`, appends a copy of the suffix of `s2` to the end of the string `s1`. Both must be properly null terminated. - **Comparing Strings**: With `strcmp(s1, s2)`, compares lexicographically the null-terminated string `s1` with the string `s2`. These functions operate directly on raw pointers representing character sequences. They require manual management of memory allocation and deallocation when dynamically allocating space for strings[^1]. ```c #include <stdio.h> #include <string.h> int main() { char dest[50]; strcpy(dest, "Hello"); strcat(dest, " World!"); printf("%s\n", dest); } ``` #### Class `std::string` Introduced in C++, `std::string` provides higher level abstractions over traditional C-style strings. This container manages its own storage automatically, allowing more intuitive manipulation without worrying about buffer sizes or overflow issues. Key features include: - **Automatic Memory Management**: Automatically resizes internal buffers as needed. - **Operator Overloading Support**: Enables natural syntax like concatenation (`+`) between objects. - **Rich Set of Member Functions**: Includes search capabilities, insertion/deletion at arbitrary positions within the sequence among others. Using `std::string` leads generally safer code since many common errors associated with pointer arithmetic are avoided[^3]. ```cpp #include <iostream> #include <string> using namespace std; int main(){ string greeting = "Hello"; greeting += " world!"; cout << greeting; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值