C++中String 最全API详解
- string构造API
- string str; //生成空的字符串
- string str =str1; //将str1赋值给str
- string str(str1); //以str1拷贝构造str,str与str1值一样
- string str(str1,pos,len); //以str1的从pos开始,len长的部分拷贝构造str
- string str(num,c); //以num个字符c拷贝构造str
- string str(str1,pos); //以str1从pos开始的部分构造str
- string str(char*beg,char*end); //以字符串的beg到end构造str
- string str(cstr); //以c语言字符串构造str
- string str(cstr,len); //以c语言字符串的前len个字符构造str
标红的三个要注意一下。
示例:
#include<iostream>
using namespace std;
int main(){
char * cstr = "123456";
string str1;
cout<<"str1 ="<<str1<<endl; //输出空
string str2 = "abcefg";
cout<<"str2 ="<<str2<<endl; //输出abcefg
string str3(str2);
cout<<"str3 ="<<str3<<endl; //输出abcefg
string str4(str3,1,3);
cout<<"str4 ="<<str4<<endl; //输出bce
string str5(7,'c');
cout<<"str5 ="<<str5<<endl; //输出ccccccc
string str6(str3,3);
cout<<"str6 ="<<str6<<endl; //输出efg
string str7(&str3[2],&str3[5]);
cout<<"str7 ="<<str7<<endl; //输出cef
string str8(cstr);
cout<<"str8 ="<<str8<<endl; //输出123456
string str9(cstr,4);
cout<<"str9 ="<<str9<<endl; //输出1234
return 0;
}
- string大小长度API
string str
- str.size(); //str对象长度,即有多少个字符
- str.length(); //str对象长度,即有多少个字符
- str.max_size(); //C++字符串最多能包含的字符数
- str.capacity(); //重新分配内存之前 string所能包含的最大字符数
- str.empty(); //判断是否为空,为空返回true,否则false
示例:
#include<iostream>
using namespace std;
int main(){
string str("12345678");
string str2;
cout<<"size = "<<str.size()<<endl; //输出8
cout<<"length = "<<str.length()<<endl; //输出8
cout<<"max_size = "<<str.max_size()<<endl; //输出4611686018427387897
cout<<"capacity = "<<str.capacity()<<endl; //输出8
if(str2.empty() == true) { //输出str is null
cout<<"str is null"<<endl;
}
return 0;
}
- string 重新设置内存大小API
string str
- str.resize(num); //resize既分配了空间,也创建了对象,可以通过下标访问,既修改capacity大小,也修改size大小。
- str.reserve(num); //reserve是设置了capacity的值,不修改size大小,但此时容器内还没有任何对象,不能通过下标访问。
示例:
#include<iostream>
using namespace std;
int main(){
string str("12345678");
cout<<"size = "<<str.size()<<endl; //输出8
cout<<"length = "<<str.length()<<endl; //输出8
cout<<"capacity = "<<str.capacity()<<endl; //输出8
str.resize(20);
cout<<"size = "<<str.size()<<endl; //输出20
cout<<"length = "<<str.length()<<endl; //输出20
cout<<"capacity = "<<str.capacity()<<endl; //输出20
str.reserve(50);
cout<<"size = "<<str.size()<<endl; //输出20
cout<<"length = "<<str.length()<<endl; //输出20
cout<<"capacity = "<<str.capacity()<<endl; //输出50
return 0;
}
- string 比较API
string str1,str2;
- 比较操作符(>,>=,<,<=,==,!=)
- compare函数 //返回值如下:0:相等 大于0 小于0
- str1.compare(str2); //str1和str2比较
- str1.compare(start,len,str2); //str1的start开始len长度与str2比较
- str1.compare(start1,len1,str2,start2,len2); //str1的start1开始len2长度与str2的start2开始len2长度比较
示例:
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdef";
string str2 = "aabbcdeef";
string str3 = "abcdef";
string str4 = "cd";
cout<<"case1: "<<str1.compare(str2)<<endl;
cout<<"case2: "<<str1.compare(str3)<<endl; //"abcdef"与"abcdef"比较,输出0
cout<<"case3: "<<str1.compare(2,2,str4)<<endl; //"cd"与"cd"比较,输出0
cout<<"case4: "<<str1.compare(0,2,str2,1,2)<<endl; //"ab"与"ab"比较,输出0
return 0;
}
- string 查找API
从前往后完全匹配查找
- int find(char c, int pos = 0) const; //从pos开始查找字符c在当前字符串的位置
- int find(const char *s, int pos = 0) const; //从pos开始查找字符串s在当前串中的位置
- int find(const char *s, int pos, int n) const; //从当前字符串pos开始查找字符串s中前n个字符在当前串中的位置
- int find(const string &s, int pos = 0) const; //从pos开始查找字符串s在当前串中的位置
从后往前完全匹配查找
- int rfind(char c, int pos = npos) const; //从pos开始从后向前查找字符c在当前串中的位置
- int rfind(const char *s, int pos = npos) const;
- int rfind(const char *s, int pos, int n = npos) const;
- int rfind(const string &s,int pos = npos) const;
从前往后第一个字符匹配查找
- int find_first_of(char c, int pos = 0) const; //从pos开始查找字符c第一次出现的位置
- int find_first_of(const char *s, int pos = 0) const;
- int find_first_of(const char *s, int pos, int n) const;
- int find_first_of(const string &s,int pos = 0) const;
从后往前第一个字符匹配查找
- int find_last_of(char c, int pos = npos) const;
- int find_last_of(const char *s, int pos = npos) const;
- int find_last_of(const char *s, int pos, int n = npos) const;
- int find_last_of(const string &s,int pos = npos) const;
从前往后第一个字符不匹配查找
- int find_first_not_of(char c, int pos = 0) const;
- int find_first_not_of(const char *s, int pos = 0) const;
- int find_first_not_of(const char *s, int pos,int n) const;
- int find_first_not_of(const string &s,int pos = 0) const;
从后往前第一个字符不匹配查找
- int find_last_not_of(char c, int pos = npos) const;
- int find_last_not_of(const char *s, int pos = npos) const;
- int find_last_not_of(const char *s, int pos, int n) const;
- int find_last_not_of(const string &s,int pos = npos) const;
示例:
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdbef";
string str2 = "cd";
char *str3 = "cd";
char *str4 = "abdbef";
char *str5 = "dg";
string str6 = "dg";
char *str7 = "ac";
string str8 = "ac";
char *str9 = "fd";
string str10 = "fd";
cout<<"从前往后完全匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.find('b',0)<<endl; //abcdbef b 输出1
cout<<"2.c字符串出现的位置为"<<str1.find(str3,0)<<endl; //abcdbef cd 输出2
cout<<"3.c字符串出现的位置为"<<str1.find(str4,0,2)<<endl; //abcdbef ab 输出0
cout<<"4.string字符串出现的位置为"<<str1.find(str2,0)<<endl; //abcdbef cd 输出2
cout<<"-----------------------------------------------"<<endl;
cout<<"从后往前完全匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.rfind('b',string::npos)<<endl; //abcdbef b 输出4
cout<<"2.c字符串出现的位置为"<<str1.rfind(str3,string::npos)<<endl; //abcdbef cd 输出2
cout<<"3.c字符串出现的位置为"<<str1.rfind(str4,string::npos,2)<<endl; //abcdbef ab 输出0
cout<<"4.string字符串出现的位置为"<<str1.rfind(str2,string::npos)<<endl; //abcdbef cd 输出2
cout<<"-----------------------------------------------"<<endl;
cout<<"从前往后第一个字符匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.find_first_of('b',0)<<endl; //abcdbef b 输出1
cout<<"2.c字符串出现的位置为"<<str1.find_first_of(str5,0)<<endl; //abcdbef dg 输出3 不完全匹配,d字符匹配了
cout<<"3.c字符串出现的位置为"<<str1.find_first_of(str5,0,2)<<endl; //abcdbef dg 输出3
cout<<"4.string字符串出现的位置为"<<str1.find_first_of(str6,0)<<endl; //abcdbef dg 输出3
cout<<"-----------------------------------------------"<<endl;
cout<<"从后往前第一个字符匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.find_last_of('b',string::npos)<<endl; //abcdbef b 输出1
cout<<"2.c字符串出现的位置为"<<str1.find_last_of(str5,string::npos)<<endl; //abcdbef dg 输出3 不完全匹配,d字符匹配了
cout<<"3.c字符串出现的位置为"<<str1.find_last_of(str5,string::npos,2)<<endl; //abcdbef dg 输出3
cout<<"4.string字符串出现的位置为"<<str1.find_last_of(str6,string::npos)<<endl; //abcdbef dg 输出3
cout<<"-----------------------------------------------"<<endl;
cout<<"从前往后第一个字符不匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.find_first_not_of('a',0)<<endl; //abcdbef a 输出0
cout<<"2.c字符串出现的位置为"<<str1.find_first_not_of(str7,0)<<endl; //abcdbef ac 输出1 不完全匹配,c字符与str1的b匹配
cout<<"3.c字符串出现的位置为"<<str1.find_first_not_of(str7,0,2)<<endl; //abcdbef ac 输出1
cout<<"4.string字符串出现的位置为"<<str1.find_first_not_of(str8,0)<<endl; //abcdbef ac 输出1
cout<<"-----------------------------------------------"<<endl;
cout<<"从后往前第一个字符不匹配查找"<<endl;
cout<<"1.字符出现的位置为"<<str1.find_last_not_of('f',string::npos)<<endl; //abcdbef f 输出5
cout<<"2.c字符串出现的位置为"<<str1.find_last_not_of(str9,string::npos)<<endl; //abcdbef fd 输出5 不完全匹配,f字符与str1的f匹配
cout<<"3.c字符串出现的位置为"<<str1.find_last_not_of(str9,string::npos,2)<<endl; //abcdbef fd 输出5
cout<<"4.string字符串出现的位置为"<<str1.find_last_not_of(str10,string::npos)<<endl; //abcdbef fd 输出5
cout<<"-----------------------------------------------"<<endl;
return 0;
}
- string 替换API
- void swap(string &str2); //将当前串与str2替换
示例:
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdbef";
string str2 = "12345";
cout<<"str1 = "<<str1<<" str2 = "<<str2<<endl;
str1.swap(str2);
cout<<"----------after swap------------"<<endl;
cout<<"str1 = "<<str1<<" str2 = "<<str2<<endl;
return 0;
}
- string 子串API
- string substr(int pos = 0,int n = npos) const; //返回从pos开始的n个字符组成的字符串
示例:
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdbef";
string str2 = str1.substr(2,2);
cout<<"str1 = "<<str1<<" str2 = "<<str2<<endl; //输出str1 = abcdbef str2 = cd
return 0;
}
- string 赋值API
- string &assign(const char *s); //用c语言风格类型字符串s赋值
- string &assign(const char *s,int n); //用c语言风格类型字符串s开始的n个字符赋值
- string &assign(const string &s); //把字符串s赋给当前字符串
- string &assign(int n,char c); //用n个字符c赋值给当前字符串
- string &assign(const string &s,int start,int n); //把字符串s中从start开始的n个字符赋给当前字符串
- string &assign(const_iterator first,const_itertor last); //把first和last迭代器之间的部分赋给字符串
示例:
#include<iostream>
using namespace std;
int main() {
string str1;
string str2 = "123";
char *str3 = "adc";
str1.assign(str3);
cout<<"str1 = "<<str1<<endl; //输出adc
str1.assign(str3,2);
cout<<"str1 = "<<str1<<endl; //输出ad
str1.assign(str2);
cout<<"str1 = "<<str1<<endl; //输出123
str1.assign(3,'a');
cout<<"str1 = "<<str1<<endl; //输出aaa
str1.assign(str2,1,2);
cout<<"str1 = "<<str1<<endl; //输出123
string::iterator first = str2.begin();
string::iterator last = str2.end();
str1.assign(first,last);
cout<<"str1 = "<<str1<<endl;
return 0;
}
- string 迭代器API
从前往后
string::iterator或string::const_iterator两种迭代器,前一种可变,后面一种是const不可变
- const_iterator begin()const; //返回string开始位置,不可改变迭代的内容
- iterator begin(); //返回string的开始位置
- const_iterator end()const; //返回string最后一个字符后面的位置,不可改变迭代的内容
- iterator end(); //返回string的最后一个字符后面的位置
从后往前
string::reverse_iterator 或string::const_reverse_iterator 两种迭代器,前一种可变,后面一种是const不可变
- const_iterator rbegin()const; //返回string的最后一个字符的位置,不可改变迭代的内容
- iterator rbegin(); //返回string的最后一个字符的位置
- const_iterator rend()const; //返回string第一个字符位置的前面的一个位置,不可改变迭代的内容
- iterator rend(); //返回string第一个字符位置的前面的位置
示例:
#include<iostream>
using namespace std;
int main() {
string str = "abcdefg";
string::iterator it1 = str.begin();
string::const_iterator it2 = str.begin();
cout<<"str.begin() = "<<*it1<<endl; //输出第一个字符a
*it1 = 'c';
cout<<"str.begin() = "<<*it1<<" str = "<<str<<endl; //输出被改变的第一个字符c
//*it2 = 'c'; //会报错,不能改变内容
it1 = str.end();
cout<<"str.end() = "<<*it1<<endl; //输出最后一个字符后面的位置,为空
string::reverse_iterator it3 = str.rbegin();
cout<<"str.rbegin() = "<<*it3<<endl; //输出最后一个字符g
it3 = str.rend();
cout<<"str.end() = "<<*it3<<endl; //输出第一个字符前面的位置,为空
return 0;
}
- string 删除API
- iterator erase(iterator first, iterator last); //删除[first,last)之间的所有字符,返回删除后迭代器的位置,last不包括
- iterator erase(iterator it); //删除it指向的字符,返回删除后迭代器的位置
- string &erase(int pos = 0, int n = npos); //删除pos开始的n个字符,返回修改后的字符串
示例:
#include<iostream>
using namespace std;
int main() {
string str = "abcdefg";
string str1 = "abcdefg";
string str2 = "abcdefg";
string::iterator first = str.begin();
string::iterator last = str.end();
string::iterator it;
last --;last --; //last指向字符f
it = str.erase(first,last); //str删除掉了abcde
cout<<"str.erase() = "<<*it<<" str = "<<str<<endl; //输出f,str = fg
string::iterator first1 = str1.begin();
first1 ++; //first1指向字符b
it = str1.erase(first1); //str删除掉b
cout<<"str1.erase() = "<<*it<<" str1 = "<<str1<<endl; //输出c,str1 = acdefg
str2 = str2.erase(2,2); //删除是下标2开始长度2 即要删除的是cd
cout<<"str2.erase(pos,n) str2 = "<<str2<<endl; //输出str2 = abefg
return 0;
}
- string 插入API
- string &insert(int pos, const char *s); //从pos开始插入c语言风格字符串,返回插入后的字符串
- string &insert(int pos, const char *s, int n); //从pos开始插入n长度的c语言风格字符串,返回插入后的字符串
- string &insert(int pos,const string &s); //从pos开始插入string字符串,返回插入后的字符串
- string &insert(int pos,const string &s, int pos, int n); //从pos开始插入string字符串的pos开始n长度部分,返回插入后的字符串
- string &insert(int pos, int n, char c); //从pos开始插入n个字符c,返回插入后的字符串
- iterator insert(iterator it, char c); //在迭代器it处前一个位置插入字符c,返回插入后迭代器的位置
- void insert(iterator it, const_iterator first, const_iterator last); //在it处前一个位置插入[first,last)之间的字符
- void insert(iterator it, int n, char c); //在迭代器it处前一个位置插入n个字符c
示例:
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdefg";
string str2 = "abcdefg";
string str3 = "abcdefg";
string str4 = "abcdefg";
string str5 = "abcdefg";
string str6 = "abcdefg";
string str7 = "abcdefg";
string str8 = "abcdefg";
char *str = "1234";
string dst ="1234";
string::iterator it;
str1.insert(1,str);
cout<<"str1.insert(1,str) str1 = "<<str1<<endl; //输出str1 = a1234bcdefg
str2.insert(2,str,2);
cout<<"str2.insert(2,str,2) str2 = "<<str2<<endl; //输出str2 = ab12cdefg
str3.insert(2,dst);
cout<<"str3.insert(2,dst) str3 = "<<str3<<endl; //输出str3 = ab1234cdefg
str4.insert(2,dst,1,2); //str4第2个位置插入dst的1开始长度2,即23
cout<<"str4.insert(2,dst,1,2) str4 = "<<str4<<endl; //输出str4 = ab23cdefg
str5.insert(1,3,'o'); //str5第1个位置插入3个‘o’
cout<<"str5.insert(1,3,'o') str5 = "<<str5<<endl; //输出str5 = aooobcdefg
it = str6.begin();
it++; //此时it指向str6的b
string::iterator it6 = str6.insert(it,'*'); //将字符*插入到it位置前,it6指向插入后的*字符
cout<<"str6.insert(it,'*') str6 = "<<str6<<" *it6 = "<<*it6<<endl; //输出str6 = a*bcdefg *it6 = *
it = str7.begin();
it++; //此时it指向str7的b
string::const_iterator first = dst.begin(); //first指向dst的起始字符1
string::const_iterator last = dst.end(); //last指向dst最后一个字符4的后一个位置
last--; //last指向dst最后一个字符4
str7.insert(it,first,last); //将dst的[first,last)字符插入到it位置前
cout<<"str7.insert(it,first,last) str7 = "<<str7<<endl; //输出str7 = a123bcdefg
it = str8.begin();
it++; //此时it指向str8的b
str8.insert(it,4,'*'); //将4个字符*插入到it位置前
cout<<"str8.insert(it,4,'*') str8 = "<<str8<<endl; //输出str8 = a****bcdefg
return 0;
}
- string 替换API
先删除后插入,返回新的字符串
- string &replace(int pos, int n,const char *s); //删除从pos开始的n个字符,然后在pos处插入串s
- string &replace(int pos, int n0,const char *s, int n); //删除pos开始的n0个字符,然后在pos处插入字符串s的前n个字符
- string &replace(int pos, int n,const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
- string &replace(int pos, int n0,const string &s, int pos, int n); //删除pos开始的n0个字符,然后在pos处插入串s中从pos开始的n个字符
- string &replace(int pos, int n0,int n, char c); //删除pos开始的n0个字符,然后在pos处插入n个字符c
- string &replace(iterator first0, iterator last0,const char *s); //把[first0,last0)之间的部分替换为字符串s
- string &replace(iterator first0, iterator last0,const char *s, int n); //把[first0,last0)之间的部分替换为s的前n个字符
- string &replace(iterator first0, iterator last0,const string &s); //把[first0,last0)之间的部分替换为串s
- string &replace(iterator first0, iterator last0,int n, char c); //把[first0,last0)之间的部分替换为n个字符c
- string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[first,last)之间的字符串
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdefg";
string str2 = "abcdefg";
string str3 = "abcdefg";
string str4 = "abcdefg";
string str5 = "abcdefg";
string str6 = "abcdefg";
string str7 = "abcdefg";
string str8 = "abcdefg";
string str9 = "abcdefg";
string str10 = "abcdefg";
char *dst = "1234";
string dst1 = "1234";
string::iterator first0;
string::iterator last0;
string::iterator first;
string::iterator last;
str1.replace(2,2,dst); //删除str1第2位开始2个长度的部分,插入dst
cout<<"str1 = "<<str1<<endl; //输出str1 = ab1234efg
str2.replace(2,2,dst,2); //删除str2第2位开始2个长度的部分,插入dst前2位
cout<<"str2 = "<<str2<<endl; //输出str2 = ab12efg
str3.replace(2,2,dst1); //删除str3第2位开始2个长度的部分,插入dst1
cout<<"str3 = "<<str3<<endl; //输出str3 = ab1234efg
str4.replace(2,2,dst1,2,2); //删除str4第2位开始2个长度的部分,插入dst1第二位开始长度为2的部分
cout<<"str4 = "<<str4<<endl; //输出str4 = ab34efg
str5.replace(2,2,6,'&'); //删除str5第2位开始2个长度的部分,插入6个字符&
cout<<"str5 = "<<str5<<endl; //输出str5 = ab&&&&&&efg
first0 = str6.begin();
last0 = str6.end();
str6.replace(first0,last0,dst); //删除str6开始到结束,插入dst
cout<<"str6 = "<<str6<<endl; //输出str6 = 1234
first0 = str7.begin();
last0 = str7.end();
str7.replace(first0,last0,dst,2); //删除str7开始到结束,插入dst的前2位
cout<<"str7 = "<<str7<<endl; //输出str7 = 12
first0 = str8.begin();
last0 = str8.end();
str8.replace(first0,last0,dst1); //删除str8开始到结束,插入dst1
cout<<"str8 = "<<str8<<endl; //输出str8 = 1234
first0 = str9.begin();
last0 = str9.end();
str9.replace(first0,last0,8,'^'); //删除str9开始到结束,插入8个字符^
cout<<"str9 = "<<str9<<endl; //输出str9 = ^^^^^^^^
first0 = str10.begin();
last0 = str10.end();
first = dst1.begin();
last = dst1.end();
str10.replace(first0,last0,first,last); //删除str10开始到结束,插入dst1的开始到结束
cout<<"str10 = "<<str10<<endl; //输出str10 = 1234
return 0;
}
- string 连接API
- string &operator+=(const string &s); //+=重载符把字符串s连接到当前字符串的结尾
- string &append(const char *s); //把c语言风格字符串s连接到当前字符串结尾
- string &append(const char *s,int n); //把c语言风格字符串s的前n个字符连接到当前字符串结尾
- string &append(const string &s); //把s字符串连接到当前字符串尾
- string &append(const string &s,int pos,int n); //把字符串s中从pos开始的n个字符连接到当前字符串的结尾
- string &append(int n,char c); //在当前字符串结尾添加n个字符c
- string &append(const_iterator first,const_iterator last); //把迭代器first和last之间的部分连接到当前字符串的结尾
#include<iostream>
using namespace std;
int main() {
string str1 = "abcdefg";
string str2 = "abcdefg";
string str3 = "abcdefg";
string str4 = "abcdefg";
string str5 = "abcdefg";
string str6 = "abcdefg";
string str7 = "abcdefg";
char *dst = "1234";
string dst1 = "1234";
string::const_iterator first = dst1.begin();
string::const_iterator last = dst1.end();
str1+=dst1; //dst1连接到str1后面
cout<<"str1 = "<<str1<<endl; //输出str1 = abcdefg1234
str2.append(dst); //dst连接到str2后面
cout<<"str2 = "<<str2<<endl; //输出str2 = abcdefg1234
str3.append(dst,2); //dst的前2个字符连接到str3后面
cout<<"str3 = "<<str3<<endl; //输出str3 = abcdefg12
str4.append(dst1); //dst1连接到str4后面
cout<<"str4 = "<<str4<<endl; //输出str4 = abcdefg1234
str5.append(dst1,2,2); //dst1从第2位开始的2个长度部分,连接到str5后面
cout<<"str5 = "<<str5<<endl; //输出str5 = abcdefg34
str6.append(4,'#'); //将4个#连接到str6后面
cout<<"str6 = "<<str6<<endl; //输出str6 = abcdefg####
str7.append(first,last); //将dst1的开始到结束连接到str7后面
cout<<"str7 = "<<str7<<endl; //输出str7 = abcdefg1234
return 0;
}
- string 字符操作API
- char &operator[](int n); //[]重载符,返回当前字符串第n个字符
- char &at(int n); //at,返回当前字符串第n个字符
- const char *data()const; //返回一个非null终止的c字符数组
- const char *c_str()const; //返回一个以null终止的c字符串
- int copy(char *s, int n, int pos = 0) const; //把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
示例:
#include<iostream>
using namespace std;
int main() {
string str = "abcdefg";
char c1 = str[3]; //取得字符d
cout<<"c1 = "<<c1<<endl;
char c2 = str.at(4);
cout<<"c2 = "<<c2<<endl; //取得字符e
const char *s1 = str.data();
cout<<"s1 = "<<s1<<endl; //输出abcdefg
const char *s2 = str.c_str();
cout<<"s2 = "<<s2<<endl; //输出abcdefg
char s3[20] = {0};
int len = str.copy(s3,4,1);
cout<<"len = "<<len<<" ---- s3 = "<<s3<<endl; //len = 4 ---- s3 = bcde
return 0;
}
- string 输入输出流处理API
通过定义ostringstream和istringstream变量实现,<sstream>头文件中,以空格符为标志
istringstream in;ostringstream out;
- getline(istream &in,string &s); //没有特殊说明,默认遇到字符'\n'停止
- getline(istream &in,string &s,char c); //遇到字符c停止
- in>>s1>>s2;
- out<<s3<<s4;
示例:
#include<iostream>
#include<sstream>
using namespace std;
int main() {
string str("hello world!");
istringstream in(str);
ostringstream out;
string s1,s2;
string s3;
string s4;
in>>s1>>s2; //将输入流,输入到s1和s2,空格是分隔符
cout<<"s1 = "<<s1<<" s2 = "<<s2<<endl; //输出s1 = hello s2 = world!
out<<s1<<s2; //输出流,s1和s2输出到输出流out,空格是分隔符
cout<<"out = "<<out.str()<<endl; //输出out = helloworld!
getline(cin,s3); //控制台输入,存入s3
cout<<"s3 = "<<s3<<endl; //输出s3 = 234
getline(cin,s4,'*'); //控制台输入,存入s4
cout<<"s4 = "<<s4<<endl; //输出s4 = 234
return 0;
}