您的位置:首页 > 编程语言 > C语言/C++

【c++】标准库中string类的使用、示例验证及注意点

2017-02-13 11:13 197 查看
/*
**************************************************************************
* File Name: string_test.c
* Function : 1)
*            2)
* Author   : bing
* Created Time: 2017年02月12日 星期日 10时19分31秒
**************************************************************************
*/

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

int main()
{
#if 0
/*类的构造函数*/
string s1("hello");
cout << s1 << endl;

string s2(s1);
cout << s2 << endl;

string s3(18, 'a');
cout << s3 << endl;

/*类的字符操作*/
cout << s1[3] << endl;
cout << s1.at(3) << endl; //都会返回字符串中第n个字符的位置,
//但 at 会提供范围检查,当越界时会抛出 out_of_range异常,
//下标运算符不提供检查访问
char src[100];
memset(src, 0, sizeof(src));
s1.copy(src, 3, 1);    //把s1中的,以1开始的3个字符拷贝到src字符数组中,
//返回值为实际拷贝到的字符数目
cout << src << endl;

/*类的特性描述*/
cout << "capacity " <<  s1.capacity() << endl; //返回当前容量
//string中不必增加内存即可存放的元素个数
//ubantu, vs2012环境下返回值为 15,redhat返回值为5("hello")
cout << "size     " <<  s1.size() << endl;  //返回当前字符串的大小
cout << "length   " <<  s1.length() << endl;//返回当前字符串的长度
#endif

#if 0
/*类的输入输出参数*/
string s1;
//cin >> s1;         //类的运算符重载
// >> 用于输入,遇到“ ”空格结束
//cout << s1 << endl;// << 用于输出
getline(cin, s1);    //全局的getline,从输入流中读取数据到 s1
//getline只能用来输入字符
cout << s1 << endl;
#endif

#if 0
/*类的赋值*/
string s2;
s2.assign("hello");
cout << s2 << endl;
s2.assign("worldfjdka", 5);//把"worldfjdka" 前5个字符赋给s5
cout << s2 << endl;

/*类的迭代器处理*/
string::iterator it;
for(it = s2.begin(); it != s2.end(); ++it) //it 必须初始化(例如:it = s1.begin()),
//否则会报段错误
//用 ++it 可以提高程序的运行效率,
//因为 ++it 相较于 it++ 重载简单
{
cout << *it << endl;
}

string s3(s2.begin(), s2.begin() + 3); //把从 s2.begin() 开始的 3 个字符赋给s3
cout << s3 << endl;

//string::const_iterator it; //不允许改变迭代内容
#endif

#if 0
/*类的连接*/
string s1("hello");

//s1.append("world"); //把字符串"world"连接到当前字符串结尾
//s1.append("world", 3);//把字符串"world"的前n个字符连接到当前字符串结尾
//s1.append("world", 2, 3);//把字符串"world"从2开始的3个字符连接到当前字符串结尾
// s1.append(3, 'a'); //在当前字符串结尾添加 3 个 'a'

string::iterator it;
s1.append(s1.begin(), s1.end()); //把迭代器begin和end之间的部分连接到当前字符串的结尾
cout << s1 << endl;
#endif

#if 0
/*类的比较*/
//string s1("hello");
//string s2;

string *s1 = new string("hello"); //指针比较不相等
string *s2 = new string("hello");

//int ret = s1.compare("hello"); //返回值为0相等
//getline(cin, s2);

if(s1 == s2)
{
cout << "equals" << endl;
}
else
{
cout << "no equals" << endl;
}
#endif

#if 0
/*类的子串*/
string s1("hello");
string s2;

s2 = s1.substr(0, 3); //返回从0开始的3个字符组成的字符串
cout << s2 << endl;

/*类的交换*/
s1.swap(s2);
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
#endif

#if 0
/*类的查找函数*/
string s1("hello world");

//string::size_type n = s1.find("wo"); //返回 字符/字符串 在当前字符串的位置(必须存在这个字符串)
//string::size_type n = s1.find("worlls", 4, 2); //从4位置开始查找"worlls"中前 2 个字符在当前字符串中的位置
//PS:所查找的字符串必须存在与 4 位置之后的字符串中
//string::size_type n = s1.find_first_of("dhskae"); //返回 字符/字符串 中最靠前的字符的位置
//(结果为:1)
//string::size_type n = s1.find_first_not_of("helle"); //返回第一个与当前字符串中字符不同的字符位置

/* last 和 first 相似,只不过 last 是从后向前查找*/
//string::size_type n = s1.find_last_of("fsfshwk"); //返回字符串"fsfshwk"中第一个出现在当前字符串中的字符位置
//string::size_type n = s1.find_last_not_of("world"); //返回字符串中第一个不再当前字符串中字符的位置

cout << n << endl;
#endif

#if 0
/*类的替换函数*/
string s1("hello");

//s1.replace(2, 1, "kk"); //从 2 位置开始删除当前字符串的 1 个字符,
//然后在 2 位置后插入"kk"

//string::iterator it;
//s1.replace(s1.begin(), s1.end(), "world"); //删除当前字符串,替换为 "world"

cout << s1 << endl;
#endif

#if 0
/*类的插入函数*/
string s1("aa");

//s1.insert(0, "hello"); //在当前字符串0位置插入"hello"(也可以是string类对象)

string::iterator it;
string::iterator ret;
ret = s1.insert(s1.end(), 'z'); //在 s1.end() 处插入字符 'z'
cout << *ret << endl;           //返回迭代器位置

cout << s1 << endl;
#endif

#if 0
/*类的删除函数*/
string s1("hellol");

string::iterator it;
//s1.erase(s1.begin(), s1.end()); //删除 begin 和 end 间的字符
//返回 删除后迭代器的位置
#if 0
/*删除相同字符的错误示例*/
for(it = s1.begin(); it != s1.end(); ++it)
{
if(*it == 'l')
{
//s1.erase(it);  //错误示例:若最后一个字符是需要删除的字符,
//则迭代器会跳过字符串结束符,进而出现段错误
//且该方法无法删除连续出现的相同字符
}
}
#endif
/*正确示例*/
for(it = s1.begin(); it != s1.end(); )
{
if('l' == *it)
{
it = s1.erase(it);
}
else
{
++it;
}
}

cout << s1 << endl;
#endif

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ string类 迭代器