상세 컨텐츠

본문 제목

C/C++ STL의 문자열 string 사용법 검색 배치 자리 관련

본문

반응형

 

 

 

 

 

=======================

=======================

=======================

 

 

 

출처: http://hong2da.tistory.com/28

 

 

rfind : 문자열 뒤에서 부터 검색
find_first_of : 주어진 문자열에서 찾는 문자가 있는 곳의 시작위치를 찾아 반환
find_last_of : 같은 문자 중 제일 마지막 위치를 찾는 함수
find_first_not_of : 입력한 문자열중에 속하는 문자 중 어떤 문자도 같지 않은 문자의 시작 위치를 찾는 함수
find_last_of : 제일 마지막을 찾는 함수

출처: http://hong2da.tistory.com/28 [세상사는 향기-너와 내가 만들어 가는 세상]

 

 

 

 

 

 

=======================

=======================

=======================

 

 

 

 

 

  std::string::find_last_of 사용 시 주의 사항

 

출처: http://mgun.tistory.com/457

 

 

출처 : http://alones.kr/blog/627

요즘 블로깅을 통 못했다. 1주일 정도.. 바쁘다는 핑계로 너무 게을러 진 것 같아서...

다시 매진해 본다.

STL의 find_last_of를 착각하고 쓴 적이 있어서 이에 대해서 써보았다.

 in my wiki: http://alones.byus.net/moniwiki/wiki.php/find_last_of?action=show


  • initial version: 2007.05.22 

목차

1 결론2 find_last_of의 가능한 착각3 마지막 matching 되는 str의 index를 찾는 경우는 rfind()를4 또 하나의 가능한 실수 size_t5 Reference

 

[edit]

1 결론 #

 

결론 부터 이야기 하면 STL의 std::string::find_last_of는 strtok와 유사하게 인자로 받는 const char* 또는 const std::string& 문자열 내의 각 char에 대한 find_last_of이다. (※ 물론, strtok는 각 char로 tokenizing을 하겠지만)

 

문자열 전체와 정확히 일치하는 마지막 위치를 찾아 주는 것이 아니고 해당 문자열의 각 char들이 마지막으로 출현하는 index를 반환하는 것이다. 
-_-;; 이 걸로 오해하고 쓰다 낭패를 당한적이 있어서... (<== 바보) rfind()를 사용해야 할 것이다.

[edit]

2 find_last_of의 가능한 착각 #

 

확장자를 검사하는 코드를 아래와 같이 find_last_of로 작성했다면 불쌍해질 것이다.

 

std::string str = "test_data.da"; // "dat" 확장자를 가지는 지 bool bDatafile = false; int nIndex = -1; // ".dat"의 위치를 찾자. // [wrong] find_last_of는 주어진 문자열 내의 // 각 char가 match되는 마지막 index를 return // 하기 때문에 str에 대해서 true if( (nIndex = str.find_last_of(".dat")) >= 0 ) { //.dat로 끝나내?? bDatafile = true; } else { //.dat로 끝나는 애가 아니네.. bDatafile = false; }
//



위 코드는 str이 "test_data.da"인 경우도 true를 return할 것이다. str에서 ".dat"의 4글자 중 a가 마지막으로 출현한 index가 11이기 때문이다.

[edit]

3 마지막 matching 되는 str의 index를 찾는 경우는 rfind()를 #

 

위와 같은 경우는 std::string::find_last_of()가 아닌 std::string::rfind()로 아래와 같이 작성해야 할 것이다.

 

std::string str = "test_data.da"; // "dat" 확장자를 가지는 지 bool bDatafile = false; int nIndex = -1; // ".dat"의 위치를 찾자. if( (nIndex = str.rfind(".dat") ) >= 0) { //.dat로 끝나내?? bDatafile = true; } else { //.dat로 끝나내느 애가 아니네.. bDatafile = false; }
//



[edit]

4 또 하나의 가능한 실수 size_t #

 

size_t가 unsigned int 라는 것을 잘 알 것이다. 하지만 이 것을 간과하고 위의 rfind()로 작성한 코드를 아래와 같이 작성한다면 -_-;;

 

rfind()가 찾지못한 결과에 대한 -1 (i.e. std::string::npos)가 unsigned int로 표현되어 항상 true가 될 것이다.

 

std::string str = "test_data.da"; // "dat" 확장자를 가지는 지 bool bDatafile = false; int nIndex = -1; // ".dat"의 위치를 찾자. // wrong rfind()는 size_t 즉 unsigned int // 를 return하기 때문에 항상 true if( str.rfind(".dat") ) { //.dat로 끝나내?? bDatafile = true; } else { //.dat로 끝나내느 애가 아니네.. bDatafile = false; } //

 

위의 경우는 아래 코드와 같이 std::string::npos를 사용하든지 int로 casting을 해서 사용해야 할 것이다.




std::string str = "test_data.da"; // "dat" 확장자를 가지는 지 bool bDatafile = false; int nIndex = -1; // ".dat"의 위치를 찾자. // size_t반환이고 -1은 std::string::npos if( str.rfind(".dat") != std::string::npos) { //.dat로 끝나내?? bDatafile = true; } else { //.dat로 끝나내느 애가 아니네.. bDatafile = false; }

//

[edit]

5 Reference #

 

 std::string::find_last_of() in cplusplus.com

 

 std::string::npos in cplusplus.com

 

 std::string::rfind in cplusplus.com

 

 basic string

 



출처: http://mgun.tistory.com/457 [Go crazy for anything that will make you smile.]

 

 

 

 

반응형

 

 

728x90

 

 

 

 

 

 

 

=======================

=======================

=======================

 

 

 

 

 

출처: http://gakari.tistory.com/entry/CC-STL-%EB%9D%BC%EC%9D%B4%EB%B8%8C%EB%9F%AC%EB%A6%AC-string-%ED%95%A8%EC%88%98-%EC%82%AC%EC%9A%A9%EB%B2%95

 

 

#include <string>
#include <iostream>
using namespace std;
int main()
{
         // 생성및 초기화
         string str1 = "ABCDE";
         string str2("FGH");
 
         string str3 = str2;
         string str4 = str1 + str2;
 
         cout << "str1= " << str1 << endl;
         cout << "str2= " << str2 << endl;
         cout << "str3= " << str3 << endl;
         cout << "str4= " << str4 << endl;
 
         //string 객체생성시인자로 문자를 지정할수는 없다.
         // char ch = 'A';
         // string str6 = ch;  //에러발생
         // string str7 = 'A'; //에러발생
         // string str8('A');  //에러발생
 
         string str8(1, 'A');  //한문자 대입은 이렇게합니다.
         string str9(str1, 2, 3);// 2번째 인덱스에서 시작하여, 3개의 단어를 대입
         cout << "str8= " << str8 << endl;
         cout << "str9= " << str9 << endl;
 
         // 길이
         string str10 = "Hello";
         string::size_type len;
         len = str10.length();
         cout << "length()= " << len << endl;
         len = str10.size();
         cout << "size()= " << len << endl;
 
         // c_str
         string str11 = "Good Job";
         char arr[15];
         strcpy(arrstr11.c_str()); // string객체에서, char 문자열 포인터로 변환은 c_str()함수로..
         cout << "arr= " << arr << endl;
 
         // insert
         string str12 = "abcdefghi";
         string str13 = "0123";
         str12.insert (3,str13); //3번째 인덱스에 str13 문자열을 삽입
         cout << "str12= " << str12 << endl// "abc0123defghi"
 
         // erase
         string str14 = "123456";
         str14.erase (2, 3); //2번째 인덱스부터, 3개의 문자를 삭제
         cout << "str14= " + str14 << endl;
 
         // replace
         string str15 = "abcdefghi";
         string str16 = "123";
         str15.replace (4,2,str16);//4번째 인덱스부터, 2개의 문자를 str16문자열로 대체..
         cout << "str15= " + str15 << endl;
 
         // find, rfind
         string str17 = "abcdefghi";
         string str18 = "cde";
         string::size_type pos = str17.find (str18, 0); //0번째 인덱스부터, "cde" 일치하는 시작위치를 리턴
         cout << "Position of find is " << pos << endl// 2  
         pos = str17.find ("123",0);
         if (pos == string::npos)
                  cout << "Not found" << endl;
 
         pos = str17.rfind (str18str17.length()-1); //str17 마지막 인덱스부터뒤에서 검색하며맨처음 일치하는 위치를리턴
         cout << "Position of rfind is " << pos << endl// 2
 
         // find_first_of, find_last_of
         string str20 = "Hello Codein";
         string str21 = "abcde";
         pos = str20.find_first_of (str21, 0); //0번째 인덱스부터, str21 문자열중 하나의 문자라도 일치하는 위치를 리턴
         cout <<  "Position of find_first_of is " << pos << endl// 1
         pos = str20.find_last_of ("abcde"); // str20 마지막 인덱스부터뒤에서 검색하며맨처음 일치하는 위치를 리턴
         cout << "Position of find_last_not_of is " << pos << endl// 11
 
 
         // substr
         string str22 = "123456789";
         string str23 = str22.substr (6,2); //6번째 인덱스부터, 2개의 문자를 취해서 리턴
         cout << "substr()= " << str23 << endl// "78"
 
         // 연산자(=, +, +=, ==, <<, >>, [])
         // "=" (= 연산자를 사용하여다른 string 객체의 문자열을 대입가능)
         string s1 = "Hello";
         string s2;
         s2 = s1;
         cout << "s1= " << s1 << ", s2= " << s2 << endl;
 
         string s3;
         s3 = "Codein!!"; // 직접 문자열을 대입가능
         cout << "s3= " << s3 << endl;
 
         string s4;
         char ch2 = 'C';
         s4 = ch2//문자 대입가능 (생성시대입은 허용되지 않음, string s4 = 'C': 잘못됨)
         s4 = 'A';
 
         // "+" 연산자
         // 두개의 string 객체에 "+" 연산자를 사용하여 연결가능
         string s5 = "Hello ";
         string s6 = "Korea";
         string s7 = s5 + s6// "Hello Korea"
         cout << "s7= " << s7 << endl;
 
         // 하나의 string 객체와 직접문자열을 "+" 연산자를 사용하여 연결가능
         string s8 = "Hello ";
         string s9 = s8 + "Mr Chin";
         cout << "s9= " << s9 << endl;
 
         // 하나의 string 객체와 문자를 "+" 연산자를 사용하여 연결가능
         string s10 = "Hello ";
         string s11 = s10  + '!';
         cout << "s11= " << s11 << endl;
 
         // "+=" 연산자
         string s12 = "Hello ";
         s12 += "*^^*";
         cout << "s12= " << s12 << endl;
 
         // "<<" 연산자
         // string 객체를 출력스트림으로 전송
         string s13 = "How are you?";
         cout << s13 << endl;
 
         // ">>" 연산자
         // 입력스트림으로 부터 문자열을 읽어서 string 객체에 저장
         string s14;
         cout << "입력후 엔터키를 눌러주세요" <<endl;
         cin >> s14;
 
         // [] 연산자
         // [] string 객체내 하나의 문자에 접근할수 있도록 해줌
         string s15 = "abcdef";
         char ch3 = s15[3];
         cout << "ch3= " << ch3 << endl;
         s15[2] = 'Z';
         cout << "s15= " << s15 << endl;
 
         return 1;
}


실행결과:



출처: http://gakari.tistory.com/entry/CC-STL-라이브러리-string-함수-사용법 [가카리의 공부방]

 

 

 

=======================

=======================

=======================

 

 

반응형


관련글 더보기

댓글 영역