vector string的应用
来源:互联网 发布:魔笛手压缩算法 编辑:程序博客网 时间:2024/05/25 18:11
把字符串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;
}
结果:bill@WebDoss:~/tmp/vector$ ./vect
her
lewjan
bill
stan
yx
her
lewjan
bill
stan
yx
例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;
}
结果:bill@WebDoss:~/tmp/vector/test$ ./n
wo shi w
wo shi w
1.string find 函数
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
函数名描述 find 查找 rfind 反向查找 find_first_of查找包含子串中的任何字符,返回第一个位置 find_first_not_of查找不包含子串中的任何字符,返回第一个位置 find_last_of查找包含子串中的任何字符,返回最后一个位置 find_last_not_of查找不包含子串中的任何字符,返回最后一个位置
函数名描述 find 查找 rfind 反向查找 find_first_of查找包含子串中的任何字符,返回第一个位置 find_first_not_of查找不包含子串中的任何字符,返回第一个位置 find_last_of查找包含子串中的任何字符,返回最后一个位置 find_last_not_of查找不包含子串中的任何字符,返回最后一个位置
2.如果你希望使用你自己定义的字符,你必须定义包含下列成员的结构:
表达式 描述 char_type字符类型 int_typeint 类型 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 从输入流中读入一行
函数名描述 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 从输入流中读入一行
- vector string的应用
- Vector,和vector的应用
- vector<char*>和vector<string>的不同
- C++之string vector bitset简单应用
- vector的应用
- vector 的应用VC++
- vector 的应用VC++
- vector的简单应用
- vector的应用
- vector 的应用VC++
- vector 的应用VC++
- vector的应用
- vector的简单应用
- vector的应用举例
- vector的应用
- vector&String的适当容量
- vector&String的适当容量
- multiset<string>与vector<string>的比较
- J2SE基础夯实系列之使用StackTraceElement封装,打印调试代码!
- SQL通用防注入系统asp版 插一句话漏洞利用
- Extjs DOM操作总结
- 自然语言处理工具(Stanford-Parser)使用注意事项
- 精心挑选的23款美轮美奂的 jQuery 图片特效插件
- vector string的应用
- skb里的数据
- Adding Dynamic Contents to IFrames
- 今天是2012年8月14日
- 使用SQL*Plus
- 【KMP】串匹配-java代码【最新版本】
- Informatica 8.6
- poj 1135 Dijskral
- Android系统升级流程