#include <string>
using namespace std;
string str = "I love ";
str.append("C++");
// 빈 문자열을 가진 스트링 객체
string str;
// 문자열 리터널로 초기화
string address("청주");
// address를 복사한 copyAddress
string copyAddress(address);
//C-스트링(char [] 배열)으로 스트링 객체 생성
char text[] = {'L','O','V','E'};
//"LOVE" 문자열을 가진 title 생성
string title(text);
cout 과 << 연산자
cout << address << endl;
cout << title << endl;
cin 과 >> 연산자
string name;
cin >> name; //공백이 입력되면 하나의 문자열로 입력
stoi() 함수 이용 2010 버전 이상
string s = "123";
int n = stoi(s);
atoi 2008 이하
string s = "123";
int n = atoi(s.c_str());
string *p = new string("c++");
cout << *p; //"c++" 출력
p -> append("Great");
cout << *p; //"c++ Great" 출력
delete p;
예제)
#include <iostream>
#include <string>
using namespace std;
int main(){
string names[5];
for (int i =0 ; i < 5 ; i++)
getline(cin,names[i],'\n');
string latter = names[0];
for(int i = 1; i < 5 ; i++){
if(latter < names[i]){
latter = names[i]
}
}
cout << "사전에서 가장 뒤에 나오는 문자열은" << latter << endl;
}
int len = s.length();
string first = s.substr(0,1);
string sub = s.substr(1, len-1);
s = sub + first;
int fIndex = s.find('+', startIndex)
getline(cin, s, '&'); // 문자열 입력
cin.ignore(); // &뒤에 따라오는 엔터를 제거하기 위한 코드
s.replace(fIndex, f.length(), r); // fIndex부터 문자열 f의 길이만큼 문자열 r로 변경