C++ベース---stringクラスのoperator+=/append/push_back
5803 ワード
1.stringクラスのoperator+=/append/push_back
1.1 std::string::operator+=プロトタイプ:string&operator+=(const string&str); 説明:string型文字列strをソース文字列の末尾に接続します. プロトタイプ:string&operator+=(const char*s); 説明:char型文字列sをソース文字列の末尾に接続する. プロトタイプ:string&operator+=(char c); 説明:文字cをソース文字列の末尾に接続します. プロトタイプ:string&operator+=(initializer_list il); 説明:An initializer_list object.These objects are automatically constructed from initializer list declarators.The characters are appended to the string, in the same order. コード例: 1.2 std::string::append プロトタイプ:string&append(const string&str); 説明:string型文字列strをソース文字列の末尾に接続します. プロトタイプ:string&append(const string&str,size_t subpos,size_t sublen=npos); では、string型文字列strのsubposとして下から始まるsublen長の文字列で現在の文字列の末尾に接続されていることを示しています. プロトタイプ:string&append(const char*s); 説明:char型文字列sを現在の文字列の末尾に接続します. プロトタイプ:string&append(const char*s,size_t n); 説明:char型文字列sで開始されたn文字が現在の文字列の末尾に接続されます. プロトタイプ:string&append(size_t n,char c); 説明:現在の文字列の最後にn文字cを追加します. プロトタイプ:templatestring&append(InputIterator first,InputIterator last); 説明:Appends a copy of the sequence of characters in the range[first,last],in the same order. プロトタイプ:string&append(initializer_list il); 説明:Appends a copy of each of the characters in il,in the same order. コード例: 1.3 std::string::push_back プロトタイプ:void push_back (char c); 説明:文字列の末尾に文字を追加します. コード例: 参考文献:[1]ネットワークリソース:http://www.cplusplus.com/reference/string/string/string/
1.1 std::string::operator+=
#include
#include
using namespace std;
int main ()
{
string name ("John");
string family ("Smith");
name += " K. "; // c-string
name += family; // string
name += '
'; // character
cout<"pause");
return 0;
}
=>John K. Smith
#include
#include
using namespace std;
int main ()
{
string str;
string str2="Writing ";
string str3="print 10 and then 5 more";
// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3, 6, 3); // "10 "
str.append("dots are cool", 5); // "dots "
str.append("here: "); // "here: "
str.append(10u, '.'); // ".........."
str.append(str3.begin()+8, str3.end()); // " and then 5 more"
str.append(5, 0x2E); // "....."
cout<"pause");
return 0;
}
=>Writing 10 dots here: .......... and then 5 more.....
#include
#include
using namespace std;
int main ()
{
string str = "hello world";
str.push_back('!');
cout<"pause");
return 0;
}
=>hello world!