C++ string类使用总结

string类

string类是一个模板类,它的定义如下:

typedef basic_string<char> string;

使用string类要包含头文件<string>

string对象的初始化
  • 正确的初始化方法
string s1("Hello");  // 一个参数的构造函数
string s2(8, ‘x’);   // 两个参数,s2中有8个x
string month = “March”;
  • 不提供以字符整数为参数的构造函数
    错误的初始化方法:
string error1 = ‘c’;  // 错
string error2(‘u’);   // 错
string error3 = 22;   // 错
string error4(8);     // 错
  • 可以将字符型赋值给string对象
string s;
s = ‘n’;
  • 构造的string太长而无法表达 -> 会抛出length_error异常
  • string对象的长度用成员函数length()读取
string s("hello");
cout << s.length() << endl;
  • string支持流读取运算符
string stringObject;
cin >> stringObject;
  • string支持getline函数
string s;
getline(cin, s);
样例程序
#include <iostream>
#include <string>
using namespace std;
int main() {
    string s1("Hello");
    cout << s1 << endl;
    string s2(8, 'x');
    cout << s2 << endl;
    string month = "March";
    cout << month << endl;
    string s;
    s='n';
    cout << s << endl;
    return 0;
}

程序输出:

Hello
xxxxxxxx
March
n
string的赋值和连接
  • =赋值
string s1("cat"), s2;
s2 = s1;
  • assign成员函数复制
string s1("cat"), s3;
s3.assign(s1);
  • assign成员函数部分复制
string s1("catpig"), s3;
s3.assign(s1, 1, 3);  //从s1中下标为1的字符开始复制3个字符给s3 s3 = "atp"
  • 单个字符复制
s2[5] = s1[3] = ‘a’;
  • 逐个访问string对象中的字符
string s1("Hello");
for(int i = 0; i< s1.length(); i++) {
    cout << s1.at(i) << endl;
    cout << s1[i] << endl;
}   

成员函数at会做范围检查, 如果超出范围, 会抛出out_of_range异常, 而下标运算符[]不做范围检查

  • +运算符连接字符串
string s1("good "), s2("morning! ");
s1 += s2;
cout << s1;   // "good morning! "
  • 用成员函数append连接字符串
string s1("good "), s2("morning! ");
s1.append(s2);    
cout << s1;
s2.append(s1, 3, s1.size());   //s1.size(), s1字符数
cout << s2;
//下标为3开始, s1.size()个字符
//如果字符串内没有足够字符, 则复制到字符串最后一个字符
比较string
  • 用关系运算符比较string的大小
  • == , >, >=, <, <=, !=
  • 返回值都是bool类型,成立返回true,否则返回false
string s1("hello"), s2("hello"), s3("hell");
bool b = (s1 == s2);
cout << b << endl;
b = (s1 == s3);
cout << b << endl;
b = (s1 > s3);
cout << b << endl;

输出:

1
0
1
  • 用成员函数compare比较string的大小
string s1("hello"), s2("hello"), s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1, 2, s3, 0, 3);   //s1 1-2; s3 0-3
int f5 = s1.compare(0, s1.size(), s3);  //s1 0-end
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;

输出:

0   // hello == hello
1   // hello > hell
-1  // hell < hello
-3  // el < hell
1   // hello > hell
子串
  • 成员函数substr()
string s1("hello world"), s2;
s2 = s1.substr(4,5);
cout << s2 << endl;   // o wor
交换string
  • 成员函数swap()
string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl;    // really
cout << s2 << endl;    // hello world
string的特性
  • 成员函数capasity()返回无需增加内存即可存放的字符数
  • 成员函数maximun_size()返回string对象可存放的最大字符数
  • 成员函数length()size()相同,返回字符串的大小/长度
  • 成员函数empty()返回string对象是否为空
  • 成员函数resize()改变string对象的长度
string s1("hello world");
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1.empty() << endl;
cout << s1 << "aaa" << endl;
s1.resize(s1.length()+10);
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1 << "aaa" << endl;
s1.resize(0);
cout << s1.empty() << endl;

输出(不同编译器上可能会不一样):

22    // capacity
18446744073709551599   // maximum_size
11   / length
11   // size
0   // empty
hello worldaaa    // string itself and "aaa"
22
18446744073709551599
21
21
hello worldaaa
1
寻找string中的字符
  • 成员函数find()
string s1("hello world");
s1.find("lo");   // 3
//在s1中从前向后查找 “lo” 第一次出现的地方
//如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标
//如果找不到, 返回 string::npos (string中定义的静态常量)

string s1("hello worlld");
cout << s1.find("ll", 1) << endl;
cout << s1.find("ll", 2) << endl;
cout << s1.find("ll", 3) << endl;
//分别从下标1, 2, 3开始查找 “ll”
  • 成员函数rfind()
string s1("hello world");
s1.rfind("lo");   // 3
//在s1中从后向前查找 “lo” 第一次出现的地方
//如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标
//如果找不到, 返回 string::npos
  • 成员函数find_first_of()
string s1("hello world");
s1.find_first_of("abcd");    // 10
//在s1中从前向后查找 “abcd” 中任何一个字符第一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
  • 成员函数find_last_of()
string s1("hello world");
s1.find_last_of(“abcd");
//在s1中查找 “abcd” 中任何一个字符最后一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
  • 成员函数find_first_not_of()
string s1("hello world");
s1.find_first_not_of("abcd");   // 0
//在s1中从前向后查找不在 “abcd” 中的字母第一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
  • 成员函数 find_last_not_of()
string s1("hello world");
s1.find_last_not_of("abcd");   // 9
//在s1中从后向前查找不在 “abcd” 中的字母第一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
string s1("hello worlld");   
cout << s1.find("ll") << endl;  // 2
cout << s1.find("abc") << endl;  // 18446744073709551615
cout << s1.rfind("ll") << endl;   // 9
cout << s1.rfind("abc") << endl;  // 18446744073709551615
cout << s1.find_first_of("abcde") << endl;  // 1
cout << s1.find_first_of("abc") << endl;   // 18446744073709551615
cout << s1.find_last_of("abcde") << endl;   // 11
cout << s1.find_last_of("abc") << endl;   // 18446744073709551615
cout << s1.find_first_not_of("abcde") << endl;   // 0 
cout << s1.find_first_not_of("hello world") << endl;  // 18446744073709551615
cout << s1.find_last_not_of("abcde") << endl;  10
cout << s1.find_last_not_of("hello world") << endl;   //18446744073709551615
替换string中的字符
  • 成员函数erase()
string s1("hello worlld");
s1.erase(5);   // 去掉下标 5 及之后的字符
cout << s1;
cout << s1.length();
cout << s1.size();

输出:

hello55
  • 成员函数find()
string s1("hello worlld");
cout << s1.find("ll", 1) << endl;
cout << s1.find("ll", 2) << endl;
cout << s1.find("ll", 3) << endl;
//分别从下标1, 2, 3开始查找 “ll”
  • 成员函数replace()
string s1("hello world");
s1.replace(2, 3, “haha");  //将s1中下标2 开始的3个字符换成 “haha”
cout << s1;

输出:

hehaha world
string s1("hello world");
s1.replace(2, 3, "haha", 1,2);
cout << s1;  
//将s1中下标2 开始的3个字符
//换成 “haha” 中下标1开始的2个字符

输出:

heah world
在string中插入字符
  • 成员函数insert()
string s1(“hello world”);
string s2(“show insert”);
s1.insert(5, s2); // 将s2插入s1下标5的位置
cout << s1 << endl;
s1.insert(2, s2, 5, 3); //将s2中下标5开始的3个字符插入s1下标2的位置
cout << s1 << endl;

输出:

helloshow insert world
heinslloshow insert world
转换成C语言式`char *`字符串
  • 成员函数c_str()
string s1("hello world");
printf("%s\n", s1.c_str());
// s1.c_str() 返回传统的const char * 类型字符串
//且该字符串以 ‘\0’ 结尾

输出:

hello world
  • 成员函数data()
string s1("hello world");
const char * p1 = s1.data();
for(int i=0; i < s1.length(); i++)
    printf("%c",*(p1+i));
//s1.data() 返回一个char * 类型的字符串
//对s1 的修改可能会使p1出错。

输出:

hello world
  • 成员函数copy()
string s1("hello world");
int len = s1.length();
char * p2 = new char[len+1];
s1.copy(p2, 5, 0);
p2[5] = '\0';
cout << p2 << endl;
// s1.copy(p2, 5, 0) 从s1的下标0的字符开始,
// 制作一个最长5个字符长度的字符串副本并将其赋值给p2
// 返回值表明实际复制字符串的长度
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C++中的string是标准库中提供的一个非常常用的,用来操作字符串。其中的find函数是用来在字符串中查找子字符串的方法。在C++中,string的find函数有多个重载版本,可以根据不同的参数型和个数进行调用。 基本的find函数签名为:size_t find(const string& str, size_t pos = 0) const。 其中,str是要查找的子字符串,pos表示开始查找的位置,默认为0。该函数的返回型为size_t,即一个无符号整数型,表示子字符串在字符串中的下标,如果找不到则返回string::npos,即-1。 下面是一个例子,演示了如何使用find函数: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); if (pos != string::npos) { cout << "子字符串\"" << subStr << "\"在位置" << pos << "找到了" << endl; } else { cout << "子字符串\"" << subStr << "\"未找到" << endl; } return 0; } ``` 在以上例子中,我们定义了一个字符串str和一个子字符串subStr,然后使用find函数在str中查找subStr。如果找到了,会打印子字符串的位置;如果未找到,则会打印未找到的提示。 需要注意的是,find函数是区分大小写的,如果要实现不区分大小写的查找,可以使用其他的函数或者自行实现。此外,find函数还有其他的重载版本,可以指定查找的方向、查找的次数等参数。不同的参数可以满足不同的需求。 ### 回答2: C++中的string是一个非常常用的字符串,它提供了许多用于处理字符串的函数,其中包括find函数。find函数的作用是在字符串中查找指定的子字符串,并返回其第一次出现的位置。 find函数有多个重载形式,最常用的形式是以下两种: 1. find(const string& str, size_t pos = 0):在字符串中从指定的位置pos开始查找子字符串str,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 2. find(const char* s, size_t pos = 0):在字符串中从指定的位置pos开始查找C风格字符串s,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 下面是一个简单的示例代码,演示了find函数的使用: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); // 查找子字符串的位置 if (pos != string::npos) { cout << "子字符串在位置 " << pos << " 处找到了。" << endl; } else { cout << "子字符串未找到。" << endl; } return 0; } ``` 运行结果为: ``` 子字符串在位置 7 处找到了。 ``` 通过查找子字符串的位置,我们可以知道子字符串是否在原字符串中以及其位置。这对于字符串的处理非常有用,例如用于查找关键字、替换子字符串等操作。 ### 回答3: C++标准库中的string提供了一个名为find的成员函数,用于在字符串中查找子字符串。它的使用方法如下: string findstr = "example"; string str = "This is an example string."; int pos = str.find(findstr); if(pos != string::npos){ cout << "子字符串在原字符串中的位置是:" << pos << endl; } else{ cout << "子字符串未在原字符串中找到。" << endl; } 以上例子中,我们首先定义了一个待查找的子字符串findstr和一个原字符串str。然后通过调用str的find函数,传入待查找的子字符串findstr作为参数。find函数返回查找到的子字符串在原字符串中的位置(的首字符索引)。如果查找成功,则返回该位置值;如果查找失败,则返回一个特定的常数string::npos。因此,我们可以通过与string::npos进行比较,判断是否找到了子字符串。 需要注意的是,find函数还可以传入另外两个可选的参数,分别是起始搜索位置和要搜索的字符数量。例如: int pos = str.find(findstr, 5, 10); 表示从原字符串的第5个字符开始,最多搜索10个字符的范围内查找子字符串findstr。 总结起来,C++ string的find函数可以用于在字符串中查找子字符串,它返回子字符串在原字符串中的位置或者一个特定的常数string::npos表示查找失败。此外,还可以通过传入可选的参数来指定起始搜索位置和搜索字符数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值