我们上次讲完了Element access,现在我们继续往下讲:
一.Modifiers:
1.operator+=:
Extends the string by appending additional characters at the end of its current value:
翻译:在string类对象结尾追加字符
如下:
#include <string>
int main()
{
//使用场景:
string s1 = "hello ";
//1.追加字符 string& operator+= (char c)
s1 += 'w';
cout <<"string& operator+= (char c):"<<s1 << endl;
//2.追加字符串 string& operator+= (const char* s)
s1 += "orld";
cout << "string& operator+= (const char* s):" << s1 << endl;
//3.追加string string& operator+= (const string& str)
string s2 = " good man";
s1 += s2;
cout << "string& operator+= (const string& str):" << s1 << endl;
return 0;
}
2.append:
Extends the string by appending additional characters at the end of its current value:
翻译:延伸string对象通过在其对象结尾追加字符(串)追加
#include <string>
int main()
{
//常用情景:
string s1("hello world");//==string s1="hello world"
//string& append (size_t n, char c);
//注意:append无:string& append (char c);
s1.append(1, '!');
cout << s1 << endl;
//string& append (const char* s);
s1.append("hello bit");
cout << s1 << endl;
//string& append (const string& str);
string s2(" apple ");
s1.append(s2);
cout << s1 << endl;
//string& append (InputIterator first, InputIterator last);
s1.append(++s2.begin(), --s2.end());//注意:改变apple间距了
cout << s1 << endl;
return 0;
}
3.push_back:
Appends character c to the end of the string, increasing its length by one.
翻译:在string对象结尾追加单个字符,从而增长其长度
该函数补充了append无:string& append (char c)的缺陷
4.assign:
Assigns a new value to the string, replacing its current contents.
翻译:将新的内容替换其之前的内容
接口和append几乎相同,所以这里不做讲解
5.insert:
Inserts additional characters into the string right before the character indicated by pos (or p):
翻译:在pos位置前插入字符串
#include <string>
int main()
{
string str = "to be question";
string str2 = "the ";
string str3 = "or not to be";
//string& insert (size_t pos, const string& str)
str.insert(6, str2);
cout << str << endl;
//string& insert (size_t pos, const string& str, size_t subpos, size_t sublen)
str.insert(6, str3, 3, 4);
cout << str << endl;
// string& insert (size_t pos, const char* s, size_t n)
str.insert(10, "that is cool", 8);
cout << str << endl;
// string& insert (size_t pos, const char* s)
str.insert(10, "to be ");
cout << str << endl;
//string& insert (size_t pos, size_t n, char c);
str.insert(15, 1, ':');
cout << str << endl;
//iterator insert (iterator p, char c)
string::iterator it = str.insert(str.begin() + 5, ',');
cout << str << endl;
str.insert(str.end(), 3, '.');
cout << str << endl;
// void insert (iterator p, InputIterator first, InputIterator last);
str.insert(it + 2, str3.begin(), str3.begin() + 3);
cout << str << endl;
return 0;
}
6.erase:
Erases part of the string, reducing its length:
翻译:消除string对象部分内容,从而减少其长度
使用,相信大家一看就会了,毕竟已经写过非常多了
7.replace:
Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the range between [i1,i2)) by new contents:
翻译:从string对象pos位置开始替换len个长度的字符串
#include <string>
int main()
{
std::string base = "this is a test string.";
std::string str2 = "n example";
std::string str3 = "sample phrase";
std::string str4 = "useful.";
std::string str = base; // "this is a test string."
str.replace(9, 5, str2); // "this is an example string." (1)
str.replace(19, 6, str3, 7, 6); // "this is an example phrase." (2)
str.replace(8, 10, "just a"); // "this is just a phrase." (3)
str.replace(8, 6, "a shorty", 7); // "this is a short phrase." (4)
str.replace(22, 1, 3, '!'); // "this is a short phrase!!!" (5)
// Using iterators: 0123456789*123456789*
str.replace(str.begin(), str.end() - 3, str3); // "sample phrase!!!" (1)
str.replace(str.begin(), str.begin() + 6, "replace"); // "replace phrase!!!" (3)
str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7); // "replace is cool!!!" (4)
str.replace(str.begin() + 12, str.end() - 4, 4, 'o'); // "replace is cooool!!!" (5)
str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end());// "replace is useful." (6)
std::cout << str << '\n';
return 0;
}
8.swap
我们对于string类中的swap要和swap函数区分:
对于后一个swap,我们以以下模版来交换不同类型,而string::swap只能交换string对象
int main()
{
string s1 = "hello world";
string s2="hello linux", s3="hello windows";
s1.swap(s2);
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
swap(s1, s3);
cout<< "s1:" << s1 << endl;
cout<< "s3:" << s3 << endl;
return 0;
}
结果:
如果我们调用string::swap还需要深拷贝,明显过于复杂,所以,我们更常用的是swap函数,这也是string过于赘余之处。
9.pop_back:
Erases the last character of the string, effectively reducing its length by one
翻译:删除string最后一个字符,对于其有效长度减少1
int main()
{
string s1("hello string ");
s1.pop_back();
cout << s1 << endl;
s1.pop_back();
cout << s1 << endl;
return 0;
}
对于modifiers我们就讲完了,里面是有非常多的是不常用的,知道用法即可,对于常用的大家就要非常清楚了。
二.Member constants
1.nops:
npos is a static member constant value with the greatest possible value for an element of type size_t.
翻译:nops是一个静态成员变量,并且能达到size_t类型的元素的最大可能值
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type
翻译:npos默认是-1,原因在于size_t是一个无符号类型,nops为默认值时,它转换为size_t时为最大值
关于nops重要的点就是这两个,大家一定要熟悉!!!
三.String operations:
1.c_str:
大家看了半天可能都不知道这个是用来干哈的,实际上当我们遇到以下情况时,就是其登场的时候:
int main()
{
string filename("Test.cpp");
FILE* fout = fopen(filename.cpp, "r");
char ch = fgetc(fout);
while (ch != EOF)
{
cout << ch;
ch = fgetc(fout);
}
return 0;
}
报错如下:
.cpp文件无法进行C语言文件读取操作,此时我们就可以用c_str()函数了
该函数解读:
Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
翻译:返回一个指向数组的指针,该数组包含一个以null结尾的字符序列(即C字符串),表示字符串对象的当前值
This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end
翻译:该数组包含了string对象的全部字符并且在结尾加上'\0'
所以,上述错误,我们就可以通过以下修改:
int main()
{
string filename("Test.cpp");
FILE* fout = fopen(filename.c_str(), "r");//将filename.cpp修改为filename.c_str()
char ch = fgetc(fout);
while (ch != EOF)
{
cout << ch;
ch = fgetc(fout);
}
return 0;
}
2.data:
其作用就是获取string对象的内容,如下:
(没啥别的作用,string缺点之一:冗杂)
3.copy:
Copy sequence of characters from string
翻译:对string对象的拷贝
注意下参数意思:
len--拷贝长度 pos拷贝起始位置
int main()
{
char buffer[20];
string str("Test string...");
size_t length = str.copy(buffer, 6, 5);
buffer[length] = '\0';
std::cout << "buffer contains: " << buffer << '\n';
return 0;
}
4.find:
Searches the string for the first occurrence of the sequence specified by its arguments
翻译:在string对象中找到第一次出现的内容(可以为字符/字符串)
该函数关键在于函数参数:
//注意点:const修饰,不要放大权限!!!
//str/s是指要找到字符串,c是要找的字符
//pos是开始查找位置
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;
//n是指查找前n个字符
size_t find (char c, size_t pos = 0) const;
int main()
{
string str("https://legacy.cplusplus.com/reference/string/string/");
string sub1, sub2, sub3;
size_t pos1 = str.find(':');
sub1 = str.substr(0, pos1 - 0);//substr是我们后面要学的,表示子string对象
cout << sub1 << endl;
size_t pos2 = str.find('/', pos1+3);
sub2 = str.substr(pos1 + 3, pos2 - (pos1 + 3));
cout << sub2 << endl;
sub3 = str.substr(pos2 + 1);
cout << sub3 << endl;
return 0;
}
结果:
4.substr:
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
翻译:返回一个新的string对象。并且其初始化通过对于string对象的一份子拷贝
5.rfind:
Searches the string for the last occurrence of the sequence specified by its arguments
翻译:在string对象中查找最后一次出现的位置
大体和find相同,不再演示,注意点为:是最后一次出现位置!!!
其他的内容,我们后面再讲,bye!!!