把字符串rusher;lewjan;bill;stan;yx拆成名字,存在vector<string>里面
例1:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string stmp;
stmp="rusher;lewjan;bill;stan;yx;";
#include <string>
#include <vector>
using namespace std;
int main()
{
string stmp;
stmp="rusher;lewjan;bill;stan;yx;";
vector<string>list;
string::size_type curpos,endpos;
curpos=0;
endpos=stmp.find_first_of(";");
string::size_type curpos,endpos;
curpos=0;
endpos=stmp.find_first_of(";");
while (endpos != string::npos)
{
string tmp=stmp.substr(curpos,endpos-curpos);
if (tmp.length()>0)
{
list.push_back(tmp);
}
curpos=endpos+1;
endpos=stmp.find(";",curpos);
}
{
string tmp=stmp.substr(curpos,endpos-curpos);
if (tmp.length()>0)
{
list.push_back(tmp);
}
curpos=endpos+1;
endpos=stmp.find(";",curpos);
}
if(stmp.length()>curpos)
{
string tmp=stmp.substr(curpos,stmp.length()-curpos);
list.push_back(tmp);
}
{
string tmp=stmp.substr(curpos,stmp.length()-curpos);
list.push_back(tmp);
}
for (unsigned int i=0;i<list.size();i++)
{
cout<<list[i]<<endl;
}
{
cout<<list[i]<<endl;
}
return 0;
}
}
结果:
bill@WebDoss:~/tmp/vector$
./e
rusher
lewjan
bill
stan
yx
rusher
lewjan
bill
stan
yx
例2:把例1写成一个函数调用
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <string>
#include <vector>
using namespace std;
bool select_del_symbol(const string &namelist,vector<string> &list)
{
string stmp;
stmp=namelist;
{
string stmp;
stmp=namelist;
string::size_type curpos,endpos; /*所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。有一点需要特别注意,所有和string::npos的比较一定要用string::size_type来使用 */
curpos=0;
endpos=stmp.find_first_of(";"); //查找包含子串中的";",返回";"所在的第一个位置
while (endpos != string::npos)
{
string tmp=stmp.substr(curpos,endpos-curpos); /*用stmp查询到第一个";"所在的位置和字符串开始位置,使用substr 来得到中间的一部分*/
if (tmp.length()>0)
{
list.push_back(tmp); //使用push_back追加字符
}
curpos=endpos+1;
endpos=stmp.find(";",curpos);
}
curpos=0;
endpos=stmp.find_first_of(";"); //查找包含子串中的";",返回";"所在的第一个位置
while (endpos != string::npos)
{
string tmp=stmp.substr(curpos,endpos-curpos); /*用stmp查询到第一个";"所在的位置和字符串开始位置,使用substr 来得到中间的一部分*/
if (tmp.length()>0)
{
list.push_back(tmp); //使用push_back追加字符
}
curpos=endpos+1;
endpos=stmp.find(";",curpos);
}
//如果最后位置没有";",需要下面的判断
if(stmp.length()>curpos)
{
string tmp=stmp.substr(curpos,stmp.length()-curpos);
list.push_back(tmp);
}
return 0;
}
if(stmp.length()>curpos)
{
string tmp=stmp.substr(curpos,stmp.length()-curpos);
list.push_back(tmp);
}
return 0;
}
int main()
{
string name="her;lewjan;bill;stan;yx;";
vector<string>listname;
select_del_symbol(name,listname);
for (unsigned int i=0;i<listname.size();i++)
{
cout<<listname[i]<<endl;
}
return 0;
}
{
string name="her;lewjan;bill;stan;yx;";
vector<string>listname;
select_del_symbol(name,listname);
for (unsigned int i=0;i<listname.size();i++)
{
cout<<listname[i]<<endl;
}
return 0;
}
例3:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string a = "wo shi wo de me";
int first = a.find_first_of ("w");
#include <string>
using namespace std;
int main ()
{
string a = "wo shi wo de me";
int first = a.find_first_of ("w");
if (first == string::npos)
{
cout << "no find any" << endl;
return -1;
}
int last = a.find_last_of ("w");
{
cout << "no find any" << endl;
return -1;
}
int last = a.find_last_of ("w");
if (last == string::npos)
{
cout << "no find" << endl;
return -1;
}
string tmp = a.substr (first, last - first + 1);
{
cout << "no find" << endl;
return -1;
}
string tmp = a.substr (first, last - first + 1);
cout << tmp << endl;
return 0;
}
return 0;
}
1.string find 函数
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
| 函数名 | 描述 |
| find | 查找 |
| rfind | 反向查找 |
| find_first_of | 查找包含子串中的任何字符,返回第一个位置 |
| find_first_not_of | 查找不包含子串中的任何字符,返回第一个位置 |
| find_last_of | 查找包含子串中的任何字符,返回最后一个位置 |
| find_last_not_of | 查找不包含子串中的任何字符,返回最后一个位置 |
2.如果你希望使用你自己定义的字符,你必须定义包含下列成员的结构:
| 表达式 | 描述 |
|---|---|
| char_type | 字符类型 |
| int_type | int 类型 |
| pos_type | 位置类型 |
| off_type | 表示位置之间距离的类型 |
| state_type | 表示状态的类型 |
| assign(c1,c2) | 把字符c2赋值给c1 |
| eq(c1,c2) | 判断c1,c2 是否相等 |
| lt(c1,c2) | 判断c1是否小于c2 |
| length(str) | 判断str的长度 |
| compare(s1,s2,n) | 比较s1和s2的前n个字符 |
| copy(s1,s2, n) | 把s2的前n个字符拷贝到s1中 |
| move(s1,s2, n) | 把s2中的前n个字符移动到s1中 |
| assign(s,n,c) | 把s中的前n个字符赋值为c |
| find(s,n,c) | 在s的前n个字符内查找c |
| eof() | 返回end-of-file |
| to_int_type(c) | 将c转换成int_type |
| to_char_type(i) | 将i转换成char_type |
| not_eof(i) | 判断i是否为EOF |
| eq_int_type(i1,i2) | 判断i1和i2是否相等 |
3.string 函数列表
| 函数名 | 描述 |
| begin | 得到指向字符串开头的Iterator |
| end | 得到指向字符串结尾的Iterator |
| rbegin | 得到指向反向字符串开头的Iterator |
| rend | 得到指向反向字符串结尾的Iterator |
| size | 得到字符串的大小 |
| length | 和size函数功能相同 |
| max_size | 字符串可能的最大大小 |
| capacity | 在不重新分配内存的情况下,字符串可能的大小 |
| empty | 判断是否为空 |
| operator[] | 取第几个元素,相当于数组 |
| c_str | 取得C风格的const char* 字符串 |
| data | 取得字符串内容地址 |
| operator= | 赋值操作符 |
| reserve | 预留空间 |
| swap | 交换函数 |
| insert | 插入字符 |
| append | 追加字符 |
| push_back | 追加字符 |
| operator+= | += 操作符 |
| erase | 删除字符串 |
| clear | 清空字符容器中所有内容 |
| resize | 重新分配空间 |
| assign | 和赋值操作符一样 |
| replace | 替代 |
| copy | 字符串到空间 |
| find | 查找 |
| rfind | 反向查找 |
| find_first_of | 查找包含子串中的任何字符,返回第一个位置 |
| find_first_not_of | 查找不包含子串中的任何字符,返回第一个位置 |
| find_last_of | 查找包含子串中的任何字符,返回最后一个位置 |
| find_last_not_of | 查找不包含子串中的任何字符,返回最后一个位置 |
| substr | 得到字串 |
| compare | 比较字符串 |
| operator+ | 字符串链接 |
| operator== | 判断是否相等 |
| operator!= | 判断是否不等于 |
| operator< | 判断是否小于 |
| operator>> | 从输入流中读入字符串 |
| operator<< | 字符串写入输出流 |
| getline | 从输入流中读入一行 |
本文详细解析了C++中字符串操作的基础知识,包括如何使用find、substr、push_back等内置函数来处理字符串,通过实例展示了如何将字符串拆分为多个元素存储于vector中,并介绍了string类提供的丰富函数列表及其应用场景。
2228

被折叠的 条评论
为什么被折叠?



