소소한 개발 공부
[프로그래머스] 특정 문자 제거하기 string.find, string.replace 본문
https://school.programmers.co.kr/learn/courses/30/lessons/120826
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
주어진 문자열 my_string에서 특정 문자 letter를 제거하는 문제이다.
나는 이 문제를 string.find 와 string.replace 를 이용해서 해결한다.
string solution(string my_string, string letter) {
while (my_string.find(letter) != string::npos)
{
my_string.replace(my_string.find(letter), letter.length(), "");
}
return my_string;
}
string.find
string.find 는 아래의 문제에서 처음 만났다.
https://soso-study.tistory.com/77
[프로그래머스] 문자열안에 문자열
https://school.programmers.co.kr/learn/courses/30/lessons/120908 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞..
soso-study.tistory.com
string.find는 문자열 안에 다른 문자열이 있는지 확인하고, 있다면 해당 문자열의 첫번째 글자의 인덱스를 반환하는 함수이다.
만약에 없다면 -1 (string::npos) 을 반환한다.
string.replace
string& replace (size_t pos, size_t len, const string& str);
문자열 안의 pos 위치부터 len 만큼을 str으로 교체하는 함수이다.
여기서 pos 위치가 잘못되면 에러가 난다. (예를 들어 -1 이나, out of range 값)
따라서 find와 같이 쓸 때는 주의해야한다.
(find는 위치를 반환하는데, -1을 반환할 수도 있기 때문에...)
string str = "strstr";
str.replace(str.find("string"), 6, ""); // -> error!
str.replace(str.find("str"), 3, ""); // -> work!
https://cplusplus.com/reference/string/string/replace/
https://cplusplus.com/reference/string/string/replace/
public member function <string> std::string::replace string (1)string& replace (size_t pos, size_t len, const string& str);string& replace (iterator i1, iterator i2, const string& str);substring (2)string& replace (size_t pos, size_t len, const string& str
cplusplus.com
string (1)
string& replace (size_t pos, size_t len, const string& str);string& replace (const_iterator i1, const_iterator i2, const string& str);
substring (2)
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos);
c-string (3)
string& replace (size_t pos, size_t len, const char* s);string& replace (const_iterator i1, const_iterator i2, const char* s);
buffer (4)
string& replace (size_t pos, size_t len, const char* s, size_t n);string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
fill (5)
string& replace (size_t pos, size_t len, size_t n, char c);string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
range (6)
template <class InputIterator> string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
initializer list (7)
string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);
동명의 다른 함수도 있는데, 아래처럼 쓸 수 있다.
std::vector<int> myvector (myints, myints+8); // 10 20 30 30 20 10 10 20
std::replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99
https://cplusplus.com/reference/algorithm/replace/?kw=replace
https://cplusplus.com/reference/algorithm/replace/?kw=replace
123456789 template void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value) { while (first!=last) { if (*first == old_value) *first=new_value; ++first; } }
cplusplus.com
'프로그래밍 > C & C++' 카테고리의 다른 글
[프로그래머스] 컨트롤 제트 split (0) | 2022.10.27 |
---|---|
[프로그래머스] 소인수분해 unique, erase (0) | 2022.10.25 |
[프로그래머스] 문자열 뒤집기 string, reverse (0) | 2022.10.21 |
[프로그래머스] 문자열안에 문자열 string.find, string::npos (0) | 2022.10.21 |
error : 'char' is promoted to 'int' when passed through '...' (0) | 2021.02.02 |