c++ STL中string的使用方法
来源:互联网 发布:网络插口怎么接线 编辑:程序博客网 时间:2024/06/03 21:30
1 <string>
string字符串尾部没有\0。
string在长度扩展后,可能会发生内存重新分配。所以,迭代器、指针在内存重新分配后会失效。
1.1 定义
string s1; 默认构造函数,s1为空串
strings2(s1); 将s2初始化为s1的一个副本
strings3("valuee"); 将s3初始化一个字符串面值副本
strings4(n,'c'); 将s4 初始化为字符'c'的n个副本
cin>>s5; 读取有效字符到遇到空格
getline(cin,s6); 读取字符到遇到换行,空格可读入,知道‘\n’结束(练习在下一个代码中)。
1.2 赋值
char *tag =“123456789”;
string str= “123”;
str.assign(10,‘c’);
str.assign(tag+1,tag+5); //两个参数a,b都是iterator,取出串[a,b)赋给str
1.3 读写
stringstr(“123”);
str[1] =‘b’; 访问单个字符
cout<< str[2];
str[100] = ‘a’; //越界访问,则行为未定义。程序可能会崩溃
str.get(100)= ‘a’; //越界访问,则会抛出out_of_range类型的异常,捕获处理后,可以让
//程序不崩溃
1.4 长度
str.empty(); 判断字符串是否为空
str.length(); 获取字符串中所含字符个数
1.5 遍历
for(int i =0; i < str.length(); i++) {
cout << str[i];
}
for(string::iterator itr= str.begin(); itr != str.end(); itr++) {
cout<< *itr;
}
1.6 连接
string strPre =“Mr. “;
string name =strPre + “Zhang” + “ Zhaoliang”;//string& operator+(const string& a,const char *);
//所以可以字符串连起来+,因为当前两个中有一个是string时,+返回的仍然是string&。
//但是“ABC” + “123”是错的,因为没有定义操作符char*加char*
name.append(“123”);//效果等同于name+=”123”;
1.7 字符判断
isalnum(c) 如果c是字母或数字,返回 true
isalpha(c) 如果c是字母,返回true
isdigit(c) 如果c是数字,返回true
islower(c) 如果c是小写字母,则为true
isupper(c) 如果c是大写字符,则为true
tolower(c) 如果c是大写字符,则返回其小写字母,否则直接返回c
toupper(c) 跟tolower相反
1.8 查找
l 查找完全匹配的字符串
string str = “hellokitty”;
3种查找目标,char, string, char*
str.find(‘e’);
str.find(str2);
str.find(“lo”);
以上是从字符串的头开始找,如果从ind处开始查找,其中,ind = [0,…,length -1]。则可以在后面加一个位置参数。
str.find(‘e’,ind);
str.find(str2,ind);
str.find(“lo”,ind);
所有形式的find函数的返回值语义一致。查找匹配时,返回字符或字符串首次出现的位置的index。无匹配内容时,返回string::npos值。
string name(“abcd1234”);
string::size_typeidx = name.find('d', 6);
if(string::npos == idx) {
cout << "Not Found!"<< endl;
} else {
cout << "index:"<< idx << endl;
}
l 查找字符串中的任一字符
stringstr=”Hello! Kitty.Well. This is is John,”
int ind = str.find_first_of(“!.”);
查找str中!、空格、.任一字符出现的第一个位置。结果是!先出现,返回5。如果是从位置10开始查找,则写成str.find_first_of(“! .”,10);
int ind =str.find_last_of(“! .”);
查找str中!、空格、.任一字符出现的最后一个位置。结果是!先出现,返回29。如果是从位置15开始向前查找,则写成str.find_last_of(“! .”, 15);
如果未找到,都是返回string::npos。
find_first_not_of,find_last_not_of是查找不在字符串中的字符,参数与find_first_of,find_last_of一样。
实现trim功能:
string s(“ \tabc \n “);
s =s.substr(s.find_first_not_of(‘));
1.9 插入
string str1 =“abcdefgh”;
str.insert(5,“abcdefgh”); //插入串“abcdefgh”
str.insert(5, str1); //插入串“abcdefgh”
str.insert(5, “abcdefgh”,3);//插入串“abcdefgh”的前3个字符
str.insert(5, str1,3); //插入串“abcdefgh”的前3个字符
1.10 删除
str.erase(3);//删除位置3的字符
str.erase(3,5);//删除位置3开始的5个字符
str.clear();//删除所有字符,length变成0
1.11 替换
str.replace (3,3 ,"good");//替换位置3开始的3个字符为good
str.swap(str2);//str和str2交换内容
1.12 串比较
str.compare (" good " ) ; s与”good“比较相等返回0,比"good"大返回1,小则返回-1;
str1 < str2; //字符串比较操作符== != <= > >=
“abc” < “abcde”, “abc” <“abd”
1.13 取子串
str.substr(2,4);//取位置2开始的4个字符,组成子串返回
1.14 与char*互转
string转[const] char*
string s ="abcdeg";
const char *k2= s1.c_str();//将内容转成const char*
const char*k2 =s1.data(); //将内容转成const char*
char k2[40];
s1.copy(k2,s.length(),0);//0是源串中的偏移量
char*转string
string str;
charchArr[]=”1234”;
str = “abcdef”;
str = chArr;
注意:只有c_str()在产生的字符串尾部加\0,data(), copy()函数都不会加。
1.15 与数值互转
数值转字符串
double price =1.234;
ostringstream o;
o << price;
string str = o.str();
字符串转数值
string strPrice = "1.234";
string strNum = "12";
const char* ch = strPrice.c_str();
const char* ch2 = strNum.c_str();
double p = atof(ch); //若格式不正确,则返回0
int num = atoi(ch2); //若格式不正确,则返回0
- c++ STL中string的使用方法
- STL第一章-string的使用方法
- STL中heap的使用方法
- STL string使用方法
- C++STL中String类的分析与运用
- C++STL中string和vector的用法
- C++:STL容器中,string操作
- STL中List的使用方法详解
- STL中Vector的使用方法详解
- STL中Vector的使用方法详解(转)
- STL中Vector的使用方法详解
- stl中优先队列的使用方法
- STL 中优先队列的使用方法(priority_queu)
- STL 中优先队列的使用方法(priority_queu)
- STL中优先队列的使用方法
- STL 中优先队列的使用方法(priority_queu)
- C++ STL中vector的使用方法
- C++ STL中map的使用方法
- 没有为类型 ViewGroup.LayoutParams 定义方法 setMargins(int, int, int)
- 关于python
- JList中添加右键菜单(JPopupMenu)
- Codeforces #257 div2 B. Jzzhu and Sequences
- Linux内核剖析 之 进程地址空间(一)
- c++ STL中string的使用方法
- 任意大小矩阵乘积
- 爱查快递API使用讲解
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum
- 人家过七夕 我们过七月半 一样一样
- hdu 2795 线段树
- scala学习久 接八
- 向前兼容与向后兼容
- httpClient使用,1 发送post请求 2 发送get请求取得接口中的数据