stringクラスの使い方
12209 ワード
コンストラクション関数:
std::string s0 ("Initial string");
//constructors used in the same order as described above: std::string s1; std::string s2 (s0); std::string s3 (s0, 8, 3);//注意位置情報は定数であり、反復器ではありません. std::string s4 ("A character sequence"); std::string s5 ("Another character sequence", 12); std::string s6a (10, 'x'); std::string s6b (10, 42); //42 is the ASCII code for '*' std::string s7 (s0.begin(), s0.begin()+7);
代入演算子のリロード(operator=):
string (1)
c-string (2)
character (3)
initializer list (4)
move (5)
反復器:
vectorの反復器と同じように、逆方向と定数反復器です.
容量:
size,length,max_size,resize,capacity,reserve,clear,empty,shrink_to_fit;
vectorに比べてlengthメソッドが1つ増え、sizeと全く同じです.
要素アクセス:
operator[], at, back, front
str.back()とstr.frontは左の値で、等号の左側でも右側でもかまいません.
変更:
operator+=(非メンバー関数operator+との違いに注意)
append(末尾に追加文字を追加することで元stringを拡張)
assign割り当ては、通常、空string Aを定義し、assignを介してAを文字列に割り当てます.
Insert関数が多く、入力パラメータはpos位置情報であってもよいし、反復器であってもよい.戻りパラメータは反復器でもstringでもよい.
string (1)
substring (2)
c-string (3)
buffer (4)
fill (5)
single character (6)
range (7)
initializer list (8)
eraseは反復器パラメータを受け入れて反復器パラメータを返し、pos位置パラメータを受け入れて修正後stringを返します.
sequence (1)
character (2)
range (3)
replace置換(戻り値はstring&)
swap交換
str1.swap(str2);
文字列アクション:
c_str stringからC型文字列への変換を提供
copyは、現在のstringの部分を宛先にコピーします.
findは文字列または文字を検索し、検索した場所を返します.注意std::string::npos(デフォルト-1)この定数は見つかりません.rfind逆検索.
find_first_of stringまたはcharを受け入れ、stringの最初の文字を見つけ、入力パラメータのいずれかのcharに一致する.
find_last_of最後に一致する位置を見つけます.
find_first_not_of
find_last_not_of
std::string s0 ("Initial string");
//constructors used in the same order as described above: std::string s1; std::string s2 (s0); std::string s3 (s0, 8, 3);//注意位置情報は定数であり、反復器ではありません. std::string s4 ("A character sequence"); std::string s5 ("Another character sequence", 12); std::string s6a (10, 'x'); std::string s6b (10, 42); //42 is the ASCII code for '*' std::string s7 (s0.begin(), s0.begin()+7);
s1:
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6a: xxxxxxxxxx
s6b: **********
s7: Initial
代入演算子のリロード(operator=):
string (1)
string& operator= (const string& str);
c-string (2)
string& operator= (const char* s);
character (3)
string& operator= (char c);
initializer list (4)
string& operator= (initializer_list il);
move (5)
string& operator= (string&& str) noexcept;
std::string str1, str2, str3;
str1 = "Test string: "; // c-string
str2 = 'x'; // single character
str3 = str1 + str2; // string
反復器:
vectorの反復器と同じように、逆方向と定数反復器です.
容量:
size,length,max_size,resize,capacity,reserve,clear,empty,shrink_to_fit;
vectorに比べてlengthメソッドが1つ増え、sizeと全く同じです.
要素アクセス:
operator[], at, back, front
str.back()とstr.frontは左の値で、等号の左側でも右側でもかまいません.
変更:
operator+=(非メンバー関数operator+との違いに注意)
std::string name ("John");
std::string family ("Smith");
name += " K. "; // c-string
name += family; // string
name += '
'; // character
append(末尾に追加文字を追加することで元stringを拡張)
std::string str;
std::string str2="Writing ";
std::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); // "....."
std::cout << str << '
';
Writing 10 dots here: .......... and then 5 more.....
push_back 尾部添加一个字符,只接受char类型。pop_back删除字符串最后一个char.
void push_back (char c);
void pop_back();
assign割り当ては、通常、空string Aを定義し、assignを介してAを文字列に割り当てます.
Insert関数が多く、入力パラメータはpos位置情報であってもよいし、反復器であってもよい.戻りパラメータは反復器でもstringでもよい.
string (1)
string& insert (size_t pos, const string& str);
substring (2)
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
c-string (3)
string& insert (size_t pos, const char* s);
buffer (4)
string& insert (size_t pos, const char* s, size_t n);
fill (5)
string& insert (size_t pos, size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
single character (6)
iterator insert (const_iterator p, char c);
range (7)
template
iterator insert (iterator p, InputIterator first, InputIterator last);
initializer list (8)
string& insert (const_iterator p, initializer_list il);
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '
';
eraseは反復器パラメータを受け入れて反復器パラメータを返し、pos位置パラメータを受け入れて修正後stringを返します.
sequence (1)
string& erase (size_t pos = 0, size_t len = npos);
character (2)
iterator erase (const_iterator p);
range (3)
iterator erase (const_iterator first, const_iterator last);
std::string str ("This is an example sentence.");
std::cout << str << '
';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
std::cout << str << '
';
// "This is an sentence."
str.erase (str.begin()+9); // ^
std::cout << str << '
';
// "This is a sentence."
str.erase (str.begin()+5, str.end()-9); // ^^^^^
std::cout << str << '
';
replace置換(戻り値はstring&)
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
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)
swap交換
str1.swap(str2);
文字列アクション:
c_str stringからC型文字列への変換を提供
const char* c_str() const noexcept;
copyは、現在のstringの部分を宛先にコピーします.
char buffer[20];
std::string str ("Test string...");
std::size_t length = str.copy(buffer,6,5);
buffer[length]='\0';
std::cout << "buffer contains: " << buffer << '
';
findは文字列または文字を検索し、検索した場所を返します.注意std::string::npos(デフォルト-1)この定数は見つかりません.rfind逆検索.
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '
';
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '
';
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '
';
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '
';
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '
';
find_first_of stringまたはcharを受け入れ、stringの最初の文字を見つけ、入力パラメータのいずれかのcharに一致する.
find_last_of最後に一致する位置を見つけます.
find_first_not_of
find_last_not_of
std::string str ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found = str.find_first_of("aeiou");//
while (found!=std::string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
substr stringの文字列を取り出します.pos位置パラメータを受け入れます.1番目は位置を表し、2番目は長さを表し、デフォルトのパラメータがあります. compare比較stringとそのパラメータ文字シーケンスが等しいかどうか.戻り値はint判定が0に等しいか否かである. 非メンバー関数: operator+ == != >= swap交換 swap(str1, str2); <> 入出力リロード getline 注意cin.get(name,20)とcin.getline(name,20)を区別して改行を破棄します.
|