c++那些事儿6 0 STL中的string

732 阅读4分钟

**知识点综述** ``` c++,string 类 string 是C++中的字符串对象,是一种特殊类型的容器,专门设计来操作的字符序列。 有容量: length()长度,max_size()最大长度 capacity存储空间大小,empty()判断字符串时候为空 访问元素: operator[]得到字符串的字符 at() 得到字符串的字符 修改: operator(+) 追加 append 追加 insert 插入 replace 替换 其它: find 查找 substr 生成字符串 compare 比较

和java的string有许多相似的功能,java应该借鉴与c++。

<hr>
**代码展示**
<hr>

#include #include #include #include using namespace std; int main() { string st; // empty()函数判断string 是否为空 std::cout << st << " " << st.empty() << endl; // string(int n,char c) ,n个字符串初始化 string st1(5, 'a'); std::cout << st1 << endl; std::cout << endl << "---------------------" << endl;

string str("logadflx"); // string构造函数。
std::cout << str << endl;
//const char &operator[](int n)const;
std::cout <<"第一个元素"<< str[0] << endl; //下标运算符,访问单个字符。
//const char &at(int n)const;
std::cout << "第3个元素" << str.at(2) << endl;

/* operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查, 当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。 */

std::cout << "str的容量:" << str.capacity() << endl;
//	看看越界情况

try
{
	str.at(16);
}
catch (const std::out_of_range& o)
{
	std::cout << "第16个元素:OutIndexArray" << endl;
}
/* string也有append()函数,和java的StringBuilder,StringBufferappend()函数相似。
  append()有许多重载函数
*/

char ch[] = { 'a','z','s','d','j','\0' }; //字符数组结尾加上'\0'不然后面会乱码。 str.append(ch); std::cout <<"追加字符串数组后:"<< str << endl; /* string 也有compare()函数 ,比较字符串的大小 compare()也有不少重载函数 与java中 compareTo(String anotherString) 相似。

*/ if (str.compare(st1)) { cout << "str字符串大于str1字符串。"<<endl; } else { cout << "str字符串小于于str1字符串。"<<endl; }

/* string substr(int pos = 0,int n = npos) const substr()函数取子串,和java中 substring()差不多。 */ cout << "取2到5的子串:" << str.substr(1, 4) << endl;

/* int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置 find()函数也有不少重载函数。 与 indexOf(int ch, int fromIndex) 相似。

*/ cout << "d出现的索引:" << str.find('d', 0)<<endl;

/* string &replace(int p0, int n0,const char *s); //删除从p0开始的n0个字符,然后在p0处插入串s replace也有许多重载函数。

/ cout << str.replace(0, 4, "\0"); cout << "str:" << str << endl; / c++ 字符串也有length属性,可以for循环遍历。 与java的length()一样。 */ std::cout <<"str对象中可存放的最大字符串的长度:"<< str.max_size() << endl; std::cout <<str<< "字符串的长度的长度:" <<str.length()<< endl;

/*
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
*/
cout << "在末尾插入3个6:" << str.insert(str.length(), 3,'6')<<endl;

/*
iterator erase(iterator first, iterator last);
//删除[first,last)之间的所有字符,返回删除后迭代器的位置
同样许多重载函数。
*/
cout << "删除6:" << str.erase(9,11)<<endl;

/*
c_str() ,返回 const 类型的指针
data() ,将内容以字符数组的形式返回
与java中 bytes()差不多。
*/
str.c_str(); //转成字符数组
int i = 0;
while (str.c_str()[i] != '\0') {
	cout << str.c_str()[i] <<" ";
	i++;
}
std::cout << endl << "--------- for循环遍历-----------" << endl;
for (int i = 0; i < str.length(); i++)  //for循环遍历
	cout << str[i] << "  ";

cout << endl << "---------iterator遍历遍历------------" << endl;
string::iterator it; //采用迭代器遍历 iterator
for (it = str.begin(); it < str.end(); it++)
{
	cout << *it <<"  ";
}
cout << endl << "---------foreach遍历遍历------------" << endl;
for (char c : str) {
	cout << c <<"  ";
}
cout <<endl<< "----------升幂排序-----------" << endl;
sort(str.begin(), str.end());
cout << str << endl;

cout << endl << "---------------------" << endl;

// sort(str.begin(), str.end(), greater()); cout << endl << "----------降幂排序-----------" << endl; cout << str.c_str() << endl;

string str1 = "asfg";
str.insert(1, str1);
cout << str << endl;


system("pause");
return 0;

}

<hr>
**结果如下**
![结果.PNG](http://upload-images.jianshu.io/upload_images/4976516-e216010b8bd0d9ff.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> **参考了几篇博客,以及工具手册**

<hr>
**工具手册,C/C++语言参考**

![C/C++语言参考部分string截图.PNG](http://upload-images.jianshu.io/upload_images/4976516-ace3ce922b0ccb6e.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<hr>
###c++的string功能其实也很强大,完全不比java差。后面的stl,和java的Collections也差不多。