개발관련/C언어

string 정리

guuuuuuu 2014. 7. 18. 16:48

#if 0

#include<string>

#include<iostream>

using namespace std;


int main()

{

string buf = "ryu gwang yeol";


cout << buf.length() << endl; 


for (int i = 0; i < 14; i++){

cout << buf[i] << endl;

}


int zz = buf.find(" "); // 공백 찾아 인덱스 리턴

cout << buf.substr(0, buf.find(" ")) << endl; // index0부터 공백까지 출력

}

#endif


#if 0

#include<iostream>

#include<string>


using namespace std;


int main()

{

string aa, bb;


aa = "zunyd";


aa += " minki"; // +로 더하기

aa.append("?"); // 끝에 추가하기


cout << aa.length() << endl;  //문자열 길이

cout << aa.size() << endl; // 문자열 사이즈 

cout << aa.capacity() << endl; //메모리 할당 크기

cout << aa << endl;

bb = aa; //bb로 문자열 카피

cout << bb << endl;


aa.clear(); //aa 버퍼 지우기   (주소는 남아있다)

cout << aa << endl;

aa.assign(bb); //bb를 aa에 할당

cout << aa << endl;

cout << bb << endl;


aa = "test";


swap(aa, bb); //aa, bb 교환

cout << aa << endl;

cout << bb << endl;


aa.insert(0, bb); //aa의 0번째 인덱스에 bb를 삽입

cout << aa << endl;

cout << bb << endl;


aa.erase(0, 4); //aa의 0번에서 4길이 만큼 삭제

cout << aa << endl;

cout << bb << endl;


aa.replace(0, 4, bb); // aa의 0번에서 4길이만큼 문자열을 bb로 치환

cout << aa << endl;

cout << bb << endl;


string cc = "abcdef";


char *dd = "adcd";


cout << aa.substr(2) << endl; // aa의 2번 인덱스부터 끝까지 가져옴

cout << aa.substr(0, 4) << endl; //aa의 0에ㅔ서 4번 이전까지 가져옴

cout << aa.find(" ") << endl; // 공백을 찾아 인덱스 리턴


bb[0] = 'z'; //한문자 치환 " "는 안된다.


string ee(10, 'a'); //10개의 배열 공간에 a로 초기화

cout << ee << endl;

string ff("adfas"); // 생성 동시에 초기화

cout << ff << endl;

string gg("agcdefghi", 4); //인덱스 4번까지 저장해서 초기화

cout << gg << endl;


cout << gg.empty() << endl; //있는지 없는지. 없을때 참


char zz[50];

strcpy(zz, gg.c_str()); // 기존 스타일로 배열 끝에 \n 추가해 복사

cout << zz << endl;


cout << "비교 : " << bool(aa == bb) << endl;

string yy("zest");

cout << "비교 : " << bool(yy == bb) << endl; //값이 같은가?

cout << "compare() : " << yy.compare(bb); //주소가 같은가?



}

#endif