char&operator[](size_tpos);// pos : 문자의 위치// 반환 : 문자열의 지정된 위치에 있는 문자
at : 해당 위치의 문자 참조를 반환
1
2
3
4
char&at(size_tpos);// pos : 문자의 위치, 문자의 위치가 아니면 out_of_range 예외가 발생// 반환 : 문자열의 지정된 위치에 있는 문자
back : 마지막 위치의 문자 참조를 반환
1
2
3
char&back();// 반환 : 문자열의 마지막 위치에 있는 문자
front : 첫번째 위치의 문자 참조를 반환
1
2
3
char&front();// 반환 : 문자열의 첫번째 위치에 있는 문자
Modifiers
operator+= : 문자열에 추가
1
2
3
4
5
6
7
8
string&operator+=(conststring&str);// string (1)string&operator+=(constchar*s);// c-string(2)string&operator+=(charc);// character (3)// str : 복사되어 마지막에 붙는 문자열// s : 복사되어 마지막에 붙는 null로 끝나는 문자 시퀀스 포인터// c : 문자열의 값에 추가되는 문자// 반환 : *this
// string (1) string&append(conststring&str);// substring (2) string&append(conststring&str,size_tsubpos,size_tsublen);// c-string (3) string&append(constchar*s);// buffer (4) string&append(constchar*s,size_tn);// fill (5) string&append(size_tn,charc);// range (6) template<classInputIterator>string&append(InputIteratorfirst,InputIteratorlast);// str : 추가하려는 다른 문자열 // subpos : 복사되는 부분 문자열의 첫번째 문자 위치// sublen : 복사할 부분 문자열의 길이// s : 문자 배열의 포인터// n : 복사하려는 문자의 개수// c : 문자 값, n번 반복// first, last : Input iterators에서 첫번째와 마지막 위치// il : initializer_list 객체// 반환 : *this
// appending to string#include <iostream>
#include <string>
intmain(){std::stringstr;std::stringstr2="Writing ";std::stringstr3="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<int>(5,0x2E);// "....."std::cout<<str<<'\n';return0;}/* output
Writing 10 dots here: .......... and then 5 more.....
*/
push_back : 문자열 마지막에 문자를 추가
1
2
3
4
voidpush_back(charc);// c : 문자열에 추가되는 문자// 반환 : 없음
// string (1) string&assign(conststring&str);// substring (2) string&assign(conststring&str,size_tsubpos,size_tsublen);// c-string (3) string&assign(constchar*s);// buffer (4) string&assign(constchar*s,size_tn);// fill (5) string&assign(size_tn,charc);// range (6) template<classInputIterator>string&assign(InputIteratorfirst,InputIteratorlast);// str : 추가하려는 다른 문자열 // subpos : 복사되는 부분 문자열의 첫번째 문자 위치// sublen : 복사할 부분 문자열의 길이// s : 문자 배열의 포인터// n : 복사하려는 문자의 개수// c : 문자 값, n번 반복// first, last : Input iterators에서 첫번째와 마지막 위치// il : initializer_list 객체// 반환 : *this
// string::assign#include <iostream>
#include <string>
intmain(){std::stringstr;std::stringbase="The quick brown fox jumps over a lazy dog.";// used in the same order as described above:str.assign(base);std::cout<<str<<'\n';str.assign(base,10,9);std::cout<<str<<'\n';// "brown fox"str.assign("pangrams are cool",7);std::cout<<str<<'\n';// "pangram"str.assign("c-string");std::cout<<str<<'\n';// "c-string"str.assign(10,'*');std::cout<<str<<'\n';// "**********"str.assign<int>(10,0x2D);std::cout<<str<<'\n';// "----------"str.assign(base.begin()+16,base.end()-12);std::cout<<str<<'\n';// "fox jumps over"return0;}/* output
The quick brown fox jumps over a lazy dog.
brown fox
pangram
c-string
**********
----------
fox jumps over
*/
// string (1) string&insert(size_tpos,conststring&str);// substring (2) string&insert(size_tpos,conststring&str,size_tsubpos,size_tsublen);// c-string (3) string&insert(size_tpos,constchar*s);// buffer (4) string&insert(size_tpos,constchar*s,size_tn);// fill (5) string&insert(size_tpos,size_tn,charc);voidinsert(iteratorp,size_tn,charc);// single character (6) iteratorinsert(iteratorp,charc);// range (7) template<classInputIterator>voidinsert(iteratorp,InputIteratorfirst,InputIteratorlast);// str : 추가하려는 다른 문자열 // subpos : 복사되는 부분 문자열의 첫번째 문자 위치// sublen : 복사할 부분 문자열의 길이// s : 문자 배열의 포인터// n : 복사하려는 문자의 개수// c : 문자 값, n번 반복// first, last : Input iterators에서 첫번째와 마지막 위치// il : initializer_list 객체// 반환 : 문자열 참조를 반환하는 경우에는 *this, // 반복자를 반환하는 경우에는 삽입된 첫번째 문자를 가리키는 반복자
// inserting into a string#include <iostream>
#include <string>
intmain(){std::stringstr="to be question";std::stringstr2="the ";std::stringstr3="or not to be";std::string::iteratorit;// used in the same order as described above:str.insert(6,str2);// to be (the )questionstr.insert(6,str3,3,4);// to be (not )the questionstr.insert(10,"that is cool",8);// to be not (that is )the questionstr.insert(10,"to be ");// to be not (to be )that is the questionstr.insert(15,1,':');// to be not to be(:) that is the questionit=str.insert(str.begin()+5,',');// to be(,) not to be: that is the questionstr.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<<'\n';return0;}/* output
to be, or not to be: that is the question...
*/
erase : 문자열에서 지정하는 위치의 문자를 제거
1
2
3
4
5
6
7
8
9
10
11
12
13
// sequence (1) string&erase(size_tpos=0,size_tlen=npos);// character (2) iteratorerase(iteratorp);// range (3) iteratorerase(iteratorfirst,iteratorlast);// pos : 지우기 시작하는 위치, 문자열 길이를 초과하면 out_of_range// len : 지우려는 문자의 개수// p : 제거될 문자 반복자// first, last : 제거할 문자열 내의 범위를 지정하는 반복자
// string (1) string&replace(size_tpos,size_tlen,conststring&str);string&replace(iteratori1,iteratori2,conststring&str);// substring (2) string&replace(size_tpos,size_tlen,conststring&str,size_tsubpos,size_tsublen);// c-string (3) string&replace(size_tpos,size_tlen,constchar*s);string&replace(iteratori1,iteratori2,constchar*s);// buffer (4) string&replace(size_tpos,size_tlen,constchar*s,size_tn);string&replace(iteratori1,iteratori2,constchar*s,size_tn);// fill (5) string&replace(size_tpos,size_tlen,size_tn,charc);string&replace(iteratori1,iteratori2,size_tn,charc);// range (6) template<classInputIterator>string&replace(iteratori1,iteratori2,InputIteratorfirst,InputIteratorlast);// str : 복사될 다른 문자열 // pos : 변경될 문자의 위치// len : 변경될 문자의 개수// subpos : 복사되는 부분 문자열의 첫번째 문자 위치// sublen : 복사할 부분 문자열의 길이// s : 문자 배열의 포인터// n : 복사하려는 문자의 개수// c : 문자 값, n번 반복// first, last : Input iterators에서 첫번째와 마지막 위치// il : initializer_list 객체// 반환 : *this
// replacing in a string#include <iostream>
#include <string>
intmain(){std::stringbase="this is a test string.";std::stringstr2="n example";std::stringstr3="sample phrase";std::stringstr4="useful.";// replace signatures used in the same order as described above:// Using positions: 0123456789*123456789*12345std::stringstr=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';return0;}/* output
replace is useful.
*/
swap : 문자열의 내용을 다른 문자열과 교환한다.
1
2
3
4
voidswap(string&str);// str : 교환할 다른 문자열// 반환 : 없음
// swap strings#include <iostream>
#include <string>
main(){std::stringbuyer("money");std::stringseller("goods");std::cout<<"Before the swap, buyer has "<<buyer;std::cout<<" and seller has "<<seller<<'\n';seller.swap(buyer);std::cout<<" After the swap, buyer has "<<buyer;std::cout<<" and seller has "<<seller<<'\n';return0;}/* output
Before the swap, buyer has money and seller has goods
After the swap, buyer has goods and seller has money
*/
size_tsize()const;size_tlength()const;// 반환 : 문자열 바이트 수
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// string::size#include <iostream>
#include <string>
intmain(){std::stringstr("Test string");std::cout<<"The size of str is "<<str.size()<<" bytes.\n";return0;}/* output
The size of str is 11 bytes
*/
resize : 문자열 길이 재설정
문자열의 길이를 변경한다.
n이 현재 문자열 길이보다 작으면 n번째 이후의 문자는 제거되며 길이가 줄어든다.
n이 현재 문자열 길이보다 크다면 필요한 만큼 문자를 삽입하여 내용을 확장한다.
c가 지정되었으면 c가 복사되고, 아니라면 null이 복사된다.
1
2
3
4
5
6
voidresize(size_tn);voidresize(size_tn,charc);// n : 새 문자열 길이// c : 새로운 공간에 넣을 문자// 반환 : 없음
empty : 문자열이 비었는지 반환
1
2
3
boolempty()const;// 반환 : 문자열 길이가 0이라면 true, 아니라면 false
String operations
substr : 부분 문자열 생성
1
2
3
4
5
6
7
stringsubstr(size_tpos=0,size_tlen=npos)const;// pos : 부분 문자열로 복제될 첫번째 문자 위치, // 문자열 길이과 같다면 함수는 빈 문자열을 반환하고, // 문자열 길이보다 크다면 out_of_range// len : 부분 문자열에 포함될 문자의 개수// 반환 : 부분 문자열인 문자열
// string::substr#include <iostream>
#include <string>
intmain(){std::stringstr="We think in generalities, but we live in details.";// (quoting Alfred N. Whitehead)std::stringstr2=str.substr(3,5);// "think"std::size_tpos=str.find("live");// position of "live" in strstd::stringstr3=str.substr(pos);// get from "live" to the endstd::cout<<str2<<' '<<str3<<'\n';return0;}/* output
think live in details.
*/
// string (1) intcompare(conststring&str)const;// substrings (2) intcompare(size_tpos,size_tlen,conststring&str)const;intcompare(size_tpos,size_tlen,conststring&str,size_tsubpos,size_tsublen)const;// c-string (3) intcompare(constchar*s)const;intcompare(size_tpos,size_tlen,constchar*s)const;// buffer (4) intcompare(size_tpos,size_tlen,constchar*s,size_tn)const;// str : 비교하려는 다른 문자열// pos : 비교를 시작하는 첫번째 문자열 위치// len : 비교하는 문자열 길이// subpos, sublen : 위의 pos, len과 같음// s : 문자 배열의 포인터// n : 비교하는 문자 개수// 반환 : 문자열 간의 관계를 나타내는 부호있는 정수
// comparing apples with apples#include <iostream>
#include <string>
intmain(){std::stringstr1("green apple");std::stringstr2("red apple");if(str1.compare(str2)!=0)std::cout<<str1<<" is not "<<str2<<'\n';if(str1.compare(6,5,"apple")==0)std::cout<<"still, "<<str1<<" is an apple\n";if(str2.compare(str2.size()-5,5,"apple")==0)std::cout<<"and "<<str2<<" is also an apple\n";if(str1.compare(6,5,str2,4,5)==0)std::cout<<"therefore, both are apples\n";return0;}/* output
green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples
*/