[C++]코딩 테스트에서 쓰이는 문자열 함수

Updated:

코딩 테스트에서는 어떤 문자열 처리 함수가 쓰일까?

지금까지 코딩테스트를 많이 본 것은 아니지만 그래도 여태까지의 코딩테스트 출제 문제들을 풀어봤을 때, 유독 많은 시간이 걸려서 푼 문제 유형이 있었다. 그것은 문자열 처리 유형. 물론 문자열을 처리하는 것이 메인인 문제들은 아니었다. 그러나 결과를 내기 위해서는 문자열 처리가 필수인 문제들이 매우 많았다는 것이다. 앞으로도 문자열 처리를 요구하는 문제들은 계속해서 나올 것이다. 그럴때마다 실시간으로 구글링해가며 C++ 문자열 처리 함수를 검색하고, 이걸 또 테스트하는 것은 매우 큰 시간 낭비이다. 오늘, 이곳에 코딩 테스트에서 자주 쓰였던 C++의 문자열 처리 함수들을 정리하고자 한다.

문자열 자르기 - string substr(size_t index, size_t len = npos) const;

  • 문자열의 index 부터 index의 문자를 포함하여 len 길이 만큼의 문자열을 반환

*len 이 문자열의 범위를 초과하면 문자열의 끝까지 반환, len 에 인자값이 없어도 문자열 끝까지 반환

string example = "012345abcde";

cout<<exmaple.substr(0, 4)<<'\n'; // ==> "0123" 
cout<<exmaple.substr(5, 100)<<'\n'; // (범위 초과) ==> "5abcde" 
cout<<exmaple.substr(5)<<'\n'; // (len 인자값 부재) ==> "5abcde"

숫자를 문자열로 변환 - string to_string(long long number)

  • 숫자형 변수 number 를 문자열로 변환하여 반환
int number = 10;

cout<<to_string(number)<<'\n'; // ==> "10" 

문자열 비교 - int compare(char* s) const

  • s 와 문자열이 같으면 0, s 보다 작으면 음수, s 보다 크면 양수
  • 그렇지만 == 를 사용해도 된다. string 클래스가 연산자 오버로딩한 함수 결과를 반환해준다
string example = "bbb";

cout<<example.compare("bbb")<<'\n'; // ==> 0
cout<<example.compare("aaa")<<'\n'; // ==> 양수 
cout<<example.compare("ccc")<<'\n'; // ==> 음수

문자열 시작 위치 찾기 - size_type find(char* s, size_t index = 0)

  • 전체 문자열에서 index 부터 시작해 s 문자열이 포함되어 있는 첫 시작 위치(인덱스)를 찾아 반환다.
  • 이놈의 경우 오버로딩 함수가 여러 개여서 가장 많이 쓰이는 오버로딩 함수를 설명한다.
string example = "012345012345";
cout<<example.find("12345")<<'\n'; // ==> 1
cout<<example.find("12345", 2)<<'\n'; // ==> 7

문자열 교체 - replace(size_type index, size_type count, const basic_string& str)

  • 전체 문자열 index 부터 index의 문자를 포함한 count 개의 문자열을 str 문자열로 대체한다.
  • 요놈도 많은 오버로딩 함수들이 존재하지만 이 형태가 가장 많이 쓰인다.

  string base = "this is a test string.";
  string str2 = "n example";
  
  base.replace(9, 5, str2);   // ==> "this is an example string."
  

문자열 지우기 -

erase(size_type index = 0, size_type count = npos) - (1)

iterator erase(const_iterator index) - (2)

iterator erase(const_iterator first, const_iterator last) - (3)

  • (1) index 부터 index의 문자를 포함하여 count 개의 문자를 지운다. count가 인자값으로 주어지지 않으면 index 부터 끝까지 지운다.
  • (2) iterator 가 매개변수가 하나이다. 이 경우는 index에 있는 ‘문자 하나만’ 지운다.
  • (3) iterator 매개변수가 둘이다. 이럴 경우 first 부터 first를 포함하여 last - 1 까지의 문자열을 삭제한다

  string example1 = "0123456789";
  string example2 = "0123456789";
  string example3 = "0123456789";
  string example4 = "0123456789";
  
  example1.erase(3, 4); 
  cout<<example1<<'\n'; // ==> "012789"
  
  example2.erase(4); 
  cout<<example2<<'\n'; // ==> "0123"
  
  example3.erase(example3.begin() + 3); // '3' 하나만 지움
  cout<<example3<<'\n'; // ==> "012456789"
  
  example4.erase(example4.begin() + 3, example4.end() - 2);
  cout<<example4<<'\n'; // ==> "01289"
  

문자열 추가하기 -

basic_string& append(size_type count, CharT ch) - (1)

basic_string& append(const basic_string& str) - (2)

basic_string& append(const basic_string& str, size_type index, size_type count = npos) - (3)

  • (1) ch 문자 하나를 count 갯수만큼 끝에 추가한다
  • (2) str 문자열을 끝에 추가한다.
  • (3) str 문자열의 index 부터 시작해 index 의 문자를 포함하여 count 개의 문자열을 추가한다. count가 npos면 index 부터 끝까지의 문자열을 추가한다.

  string example1 = "012345";
  string example2 = "abcde";
  string example3 = "fghi";
  string example4 = "hello";
  string example5 = "** world!****";
  
  example1.append(3, '*'); 
  cout<<example1<<'\n'; // ==> "012345***"
  cout<<example2.append(example3)<<'\n'; // ==> "abcdefghi";
  cout<<example4.append(example5, 2, 7)<<'\n'; // ==> "hello world!";
  

Categories:

Updated:

Leave a comment