C++风格
//初始化成字符串
string str("hello string");
cout << str << endl;
//初始化 10个,字符c
string str1(10,'c');
cout << str1 << endl;
//实例化-初始化
string str2(str1);
cout << str2 << endl;
//默认初始化,调用默认构造函数
string str4();
C语言风格
string str3 = "hello,string!!!";
cout << str3 << endl;
容量会比自己扩容的大一些
区别就是resize会改变size 也会改变capacity ,reserve只改变capacity
string str("hello string");
int len1 = str.size();
int len2 = str.length();
//包含\0
// len1=12
// len2=12
string str("hello string");
string str1 = "hello,string!!!";
str = str1;
// 输出
// hello,string!!!
string str("baby");
string str1 = "hello,string!!!";
string str2 = "xxxxxxx";
string str3 = str + str2;
//输出
//babyhello,string!!!
str += str2;
str += "abcdefg";
//输出
//babyxxxxxxxabcdefg
string& append (const string& str);
string& append (const string& str, size_t subpos, size_t sublen);//(str,str下标位置,str下标后sublen个)
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
template <class InputIterator>
string& append (InputIterator first, InputIterator last);
//例:
string str("hello");
str.append("pangrams are cool", 10,2);//获取下标10,后两个字节
//输出:
// "hellore"
string& assign (const string& str);
string& assign (const string& str, size_t subpos, size_t sublen);//str,str下标位置,截取str下标后sublen个
string& assign (const char* s);
string& assign (const char* s, size_t n);
string& assign (size_t n, char c);
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
//例:
string str;
str.assign("pangrams are cool", 10,2);
//输出:
// "re"
只能插入字符
string str("hello");
str.push_back('3');
//输出
//hello3
string str("hello");
str.push_back('113');
//这里会用最后一个来作为尾插数据
//输出
//hello3
string& erase (size_t pos = 0, size_t len = npos);//(起始下标,删除len个)
iterator erase (iterator p);
iterator erase (iterator first, iterator last);
//举例
string str("hello");
str.erase(0,3);//0位置删除3个
//输出:
//lo
string& replace (size_t pos, size_t len, const string& str);
string& replace (iterator i1, iterator i2, const string& str);
string& replace (size_t pos, size_t len, const string& str,size_t subpos, size_t sublen);//解释
string& replace (size_t pos, size_t len, const char* s);
string& replace (iterator i1, iterator i2, const char* s);
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (iterator i1, iterator i2, const char* s, size_t n);
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (iterator i1, iterator i2, size_t n, char c);
template <class InputIterator>
string& replace (iterator i1, iterator i2,InputIterator first, InputIterator last);
//举例
string str("hello");
string str1("xxxx");
str.replace(3, 2, str1, 2, 2);//在str中下标2后取2个字符,与str下标3位置后两个进行替换
//输出:
//helxx
string str("hello");
string str1("string");
str.swap(str1);
//输出:
//str=string
//str1=hello
string str("hello");
str.pop_back();
//输出:
//hell
string str = "Hellostring";
str.substr() //Hellostring
str.substr(0, 3) //hel
str.substr(2, 20)//llostring
str.substr(20, 2) //抛异常
size_t copy (char* s, size_t len, size_t pos = 0) const;
//(目标空间,拷贝长度,起始下标)不含\0
//举例
char s[20];
string str("hello string!!!");
size_t length = str.copy(s, 6, 5);
s[length] = '\0';
//输出
//strin
找到返回下标,找不到返回-1 下标往后找
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
//举例
string str("hello string!!!");
size_t len=str.find("ell");
//输出:
//1
找到返回下标,找不到返回-1,从下标开始找,往前找
string str("hello string!!!");
size_t len=str.rfind("!",3);
//返回-1
size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const;
//举例
string str("Please, replace");
size_t found = str.find_first_of("aeiou");
while (found != string::npos)//npos=-1,无符号最大值
{
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
//输出:
//Pl**s*, r*pl*c*
find_last_of反向匹配查找
如果不匹配就输出
string str("Please, replace");
size_t found = str.find_first_not_of("aeiou");
while (found != string::npos)
{
cout << str[found];
found = str.find_first_not_of("aeiou",found+1);
}
//输出:
//Pls, rplc
find_last_not_of反向不匹配查找
string str("hello string");
count<<str[2]<<endl;
count<<str.at(2)<<endl;
begin开始
end结束
迭代器的使用
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
//举例
string str = "We think in generalities, but we live in details.";
string::iterator it = str.begin();
while (it != str.end())
{
cout << *it;
it++;
}
//输出:We think in generalities, but we live in details.
反向迭代器
reverse_iterator rbegin();
const_reverse_iterator rbegin() const
reverse_iterator rend();
const_reverse_iterator rend() const
//举例
string str = "We think in generalities, but we live in details.";
string::reverse_iterator rit = str.rbegin();
while (rit != str.rend())
{
cout << *rit;
rit++;
}
//输出:.sliated ni evil ew tub ,seitilareneg ni kniht eW