void push_back( char ch);
后附字符到结尾
size()>max_size()抛出异常
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "abc";
s.push_back('d');
cout << s << endl;
return 0;
}
void pop_back( );
erase(end() - 1 ,1);
从字符串移除末字符
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "abc";
s.pop_back();
cout << s << endl;
return 0;
}
append()
后附字符到字符串
#include <string>
#include <iostream>
using namespace std;
int main() {
basic_string<char> str = "string";
const char* cptr = "C-string";
const char carr[] = "Two and one";
string output;
// 1) 后附 char 3 次。
// 注意,这是仅有的接受 char 的重载。
output.append(3, '*');//***
std::cout << "1) " << output << "\n";
// 2) 后附整个 string
output.append(str);//***string
std::cout << "2) " << output << "\n";
// 3) 后附字符串的一部分[3,3+3)ing
output.append(str, 3, 3);//***stringing
std::cout << "3) " << output << "\n";
// 4) 后附 C 字符串的一部分
// 注意,因为 `append` 返回 *this ,我们能一同链式调用
output.append(1, ' ').append(carr, 4);//***stringing Two
std::cout << "4) " << output << "\n";
// 5) 后附整个 C 字符串
output.append(cptr);//***stringing Two C-string
std::cout << "5) " << output << "\n";
// 6) 后附范围
output.append(std::begin(carr) + 3, std::end(carr));//***stringing Two C-string and one
std::cout << "6) " << output << "\n";
// 7) 后附 initializer_list
output.append({ ' ', 'l', 'i', 's', 't' });//***stringing Two C-string and one list
std::cout << "7) " << output << "\n";
}