C++ String ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ฉ”์„œ๋“œ

2023. 2. 6. 22:48ใ†๐Ÿ“š Algorithm/๐Ÿ‘€ C++ ๋ฌธ์ œ ํ’€์ด

๋ฐ˜์‘ํ˜•
using namespace std;

int main() {
	string a = "love is";
	a += "pain!";
	a.pop_back();
	cout << a << " : " << a.size() << "\n"; // love is pain : 12
	cout << char(*a.begin()) << '\n'; // L
	cout << char(*(a.end()-1)) << '\n'; // n

	a.insert(0, "test ");
	cout << a << " : " << a.size() << "\n"; // test love is pain : 17
	a.erase(0, 5);
	cout << a << " : " << a.size() << "\n"; // love is pain : 12

	auto it = a.find("love");
	if (it != string::npos) {
		cout << "ํฌํ•จ๋˜์–ด ์žˆ๋‹ค" << "\n"; // ํฌํ•จ๋˜์–ด ์žˆ๋‹ค
	}
	cout << it << "\n"; // 0
	cout << string::npos << "\n"; // 18446744073709551615

	cout << a.substr(5,2) << "\n"; // is
	return 0;

}
  • += : ๋ฌธ์ž์—ด์— ๋ฌธ์ž์—ด์„ ๋”ํ•  ๋•Œ ์‚ฌ์šฉ
    • push_back() ์€ ๋ฌธ์ž ๋ฐ–์— ๋”ํ•˜์ง€ ๋ชปํ•จ
  • begin() : ๋ฌธ์ž์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ
  • end() : ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ๊ทธ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ
  • size() : ๋ฌธ์ž์—ด ์‚ฌ์ด์ฆˆ ๋ฐ˜ํ™˜
    • O() ์‹œ๊ฐ„๋ณต์žก๋„
  • str.length() : ๋ฌธ์ž์—ด ์‚ฌ์ด์ฆˆ ๋ฐ˜ํ™˜
  • insert(์œ„์น˜, ๋ฌธ์ž์—ด) : ํŠน์ • ์œ„์น˜์— ๋ฌธ์ž์—ด ์‚ฝ์ž…
    • O(n) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„
  • erase(์œ„์น˜, ํฌ๊ธฐ)
    • ํŠน์ • ์œ„์น˜์— ํฌ๊ธฐ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ง€์›€
    • O(n) ์‹œ๊ฐ„๋ณต์žก๋„
  • pop_back() : ๋ฌธ์ž์—ด ๋์„ ์ง€์šด๋‹ค
    • O(1) ์‹œ๊ฐ„๋ณต์žก๋„
  • find(๋ฌธ์ž์—ด) : ํŠน์ • ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜
    • ๋ฌธ์ž์—ด์„ ๋ชป์ฐพ์„ ๊ฒฝ์šฐ string::npos ๋ฐ˜ํ™˜
    • O(n) ์‹œ๊ฐ„๋ณต์žก๋„
  • substr(์œ„์น˜, ํฌ๊ธฐ) : ํŠน์ • ์œ„์น˜์—์„œ ํฌ๊ธฐ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ถ”์ถœ
    • O(n) ์‹œ๊ฐ„๋ณต์žก๋„

 

์•„์Šคํ‚ค ์ฝ”๋“œ์™€ ๋ฌธ์ž์—ด

a ~ z ๊ฐœ์ˆ˜ : 26

a ~ z = 97 ~ 122

A ~ Z = 65 ~ 90

 

๋ฌธ์ž์—ด -> ์ˆซ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

int main() {
	char a = 'a';
	cout << (int)a << '\n'; // 97
	cout << (int)a - 97 << '\n'; // 0
	cout << (int)a - 'a' << '\n'; // 0

}

์ˆซ์ž ASCII ์ฝ”๋“œ -> ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

cout << char(i + 97) // 'a'
cout << char(j + 65) // 'A'

 

์ˆซ์ž๋กœ๋œ ๋ฌธ์ž์— ++ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž๋กœ 1์„ ๋”ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด?

  • ์•„์Šคํ‚ค์ฝ”๋“œ์— +1 ํ•œ ๊ฐ’์ด ๋จ!
string s = "123";
s[0]++;
cout << s << "\n"; // 223

-> s[0] ++ ์œผ๋กœ, 223์ด ๋œ๋‹ค

  • ์•„์Šคํ‚ค ์ฝ”๋“œ 49์—์„œ 1์„ ๋”ํ•œ ๊ฐ’์ธ '50'์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์ด '2' ์ด๋ฏ€๋กœ
  • 123 ์—์„œ 223์ด ๋  ์ˆ˜ ์žˆ๋‹ค
  • ๋ฌธ์ž์—ด์—์„œ + ํ•˜๋Š” ์—ฐ์‚ฐ์€ ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค

 

๋ฌธ์ž์—ด ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๊ธฐ STL - reverse()

using namespace std;
int main(){
    string a = "It's hard to have a sore leg";
    reverse(a.begin(), a.end());
    cout << a << '\n';
    reverse(a.begin(), a.end());
    cout << a << '\n';
    reverse(a.begin() + 3, a.end());
    cout << a << '\n';
    return 0;
}
/*
gel eros a evah ot drah s'tI
It's hard to have a sore leg
It'gel eros a evah ot drah s
*/

 

 

๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ split()

C++์—์„œ๋Š” split ํ•จ์ˆ˜๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค!

 

vector<string> split(string input, string delimiter) {
	vector<string> ret;
	long long pos = 0;
	string token = "";
	while((pos = input.find(delimiter)) != string::npos) {
		token = input.substr(0, pos);
		ret.push_back(token);
		input.erase(0, pos + delimiter.length());
	}

	ret.push_back(input);
	return ret;

}

input - ๋ฌธ์ž์—ด

delimiter - ๊ตฌ๋ถ„์ž

๊ตฌ๋ถ„์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ input ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„์ž ์ „๊นŒ์ง€ ์ž๋ฅธ ํ›„, 'token'๊ฐ’์— ๋„ฃ๋Š”๋‹ค.

๊ทธ๊ฑธ ret ๋ฒกํ„ฐ์— ๋„ฃ์€ ํ›„, input ๋ฌธ์ž์—ด์—์„œ ์ง€์›Œ์ค€๋‹ค.

 

 

๋Œ€๋ฌธ์ž -> ์†Œ๋ฌธ์ž / ์†Œ๋ฌธ์ž -> ๋Œ€๋ฌธ์ž ๋ณ€ํ™˜

๋ฌธ์ž์—ด์—์„œ + , - ๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘๋˜๋ฏ€๋กœ

๋ฌธ์ž์—ด ์ž์ฒด๋ฅผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์ฃผ์–ด ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค!

if ((str >= 'a') && (str <= 'z')) {
    answer.emplace_back(str - 'a' + 'A');
} else {
    answer.emplcae_back(str - 'A' + 'a');
}

 

 

 

๋ฌธ์ž์—ด ์ˆซ์ž -> ์ˆซ์ž๋กœ ๋ณ€ํ™˜

#include <iostream>


int main() {
	int num = stoi("1234"); // 1234
}
  • stoi : string to integer
  • stol : string to long int
  • stoul : string to unsigned integer
  • stoll : long long
  • stoull : unsigned long long
  • stof : string to float
  • stod : string to double
  • stold : long double

 

์ˆซ์ž -> ๋ฌธ์ž์—ด ์ˆซ์ž๋กœ ๋ณ€ํ™˜

#include <string>

std::string s = std::to_string(10); // '10'


#include <string>

int num2 = s - '0';
๋ฐ˜์‘ํ˜•

'๐Ÿ“š Algorithm > ๐Ÿ‘€ C++ ๋ฌธ์ œ ํ’€์ด' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

C++ ๋ˆ„์ ํ•ฉ  (0) 2023.02.10
C++ ์ˆœ์—ด(permutation)๊ณผ ์กฐํ•ฉ(combination)  (0) 2023.02.01
0. C++ ์ž…์ถœ๋ ฅ  (0) 2023.01.27