๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[๊ฐ์ฒด ํฌ์ธํฐ์ ๊ฐ์ฒด ๋ฐฐ์ด, ๊ฐ์ฒด์ ๋์ ์์ฑ] ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ ๋ณธ๋ฌธ
[๊ฐ์ฒด ํฌ์ธํฐ์ ๊ฐ์ฒด ๋ฐฐ์ด, ๊ฐ์ฒด์ ๋์ ์์ฑ] ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ
์ง์ง์ํ์นด 2024. 2. 22. 01:18<ํฉ๊ธฐํ ๋์ ๋ช ํ C++ Programming ์์ ์ ์ฐธ๊ณ ํด์ ์์ฑํ์์ต๋๋ค :-)>
๐ ๋ฌธ์์ด ์นํ
string a = "hi";
string b = "hello";
a = b;
๐ ๋ฌธ์์ด ๋น๊ต compare
๋ ๋ฌธ์์ด์ด ๊ฐ์ผ๋ฉด 0, str ๋ณด๋ค ์ฌ์ ์์ผ๋ก ์์ ์ค๋ฉด ์์, ๋ค์ ์ค๋ฉด ์์
string name = "gani";
string alias = "tani";
int res = name.compare(alias);
if (res == 0) cout << "๋ฌธ์์ด ๊ฐ์" << endl;
else if (res < 0) cout << name << " < " << alis << endl;
else cout << alis << " < " << name << endl;
๐ ๋ฌธ์์ด ์ฐ๊ฒฐ append
string a("I love you");
string b(".");
string c;
c = a + b;
c += b;
๐ ๋ฌธ์์ด ์ฝ์ insert, replace
string a("I love you");
a.insert(2, "really");
a.replace(2, 11, "study");
๐ ๋ฌธ์์ด ๊ธธ์ด length, size, capacity
๋ฌธ์์ด์ ํฌํจ๋ ๋ฌธ์ ๊ฐ์
length() ์ size() ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋ฆฌํดํ๋ค
capacity() ๋ ๊ฐ์ฒด์ ๋ด๋ถ ๋ฉ๋ชจ๋ฆฌ ์ฉ๋์ ๋ฆฌํดํ๋ค
string a("I love you");
int length = a.length();
int size - a.size();
int capacity = a.capacity();
๐ ๋ฌธ์์ด ์ญ์ erase, clear
arase() ๋ ๋ฌธ์์ด ์ผ๋ถ๋ถ ์ญ์
clear() ์ ์์ ํ ์ญ์
string b = "I love C++");
a.eras(0, 7);
a.clear();
๐ ์๋ธ์คํธ๋ง
๋ฌธ์์ด์์ ์ผ๋ถ๋ถ์ ๋ฐ์ทํ ๋ฌธ์์ด (์๋ธ์คํธ๋ง)์ ์ป๋๋ค
substr() ์ ๋ฌธ์์ด ๋ณํ๊ฐ ์์
string b = "I love C++");
string c = b.substr(2, 4);
string d = b.usbstr(2);
๐ ๋ฌธ์์ด ๊ฒ์
string a = "I love C++");
int index = a.find("love");
index = a.find("love", index 3);
๐ ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์ ๋ค๋ฃจ๊ธฐ
at() ํจ์์ [] ์ฐ์ฐ์๋ ๋ฌธ์์ด์ ํน์ ์์น์ ์๋ ๋ฌธ์๋ฅผ ๋ฆฌํดํ๋ค
string a = "I love C++");
char ch1 = a.at(7);
char ch2 = a[7];
a[7] = "D";
๐ ๋ฌธ์์ด์ ์ซ์ ๋ณํ, stoi
string year = "2024";
int n = stoi(year);
๐ ๋ฌธ์ ๋ค๋ฃจ๊ธฐ
<locale> ์ ์๋ touuper, isdigit, isalpah ํจ์๋ก ๋ฌธ์ ๋ค๋ฃจ๊ธฐ
string a = "hello";
for (int i = 0; i < a.length(); i++) { a[i] = toupper(a[i]); }
cout << a;
if (isdigit(a[0])) cout << "์ซ์";
else if (isalpha(a.at(0))) cout << "๋ฌธ์";
๐ ๋ฌธ์์ด ์์ฉ ๋ฌธ์
// ๋ฌธ์์ด์ ์
๋ ฅ๋ฐ๊ณ ํ์ ์ํค๊ธฐ
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "์๋์ ๋ฌธ์์ด ์
๋ ฅํ์ธ์." << endl;
getline(cin, s, '\n');
for (int i = 0; i < s.length(); i++)
{
string first = s.substr(0, 1);
string sub = s.substr(1, s.length() - 1);
s = sub + first;
cout << s << endl;
}
}
/*
I love you
love youI
love youI
ove youI l
ve youI lo
e youI lov
youI love
youI love
ouI love y
uI love yo
I love you
*/
// ๋ฌธ์์ด ์ฒ๋ฆฌ ์์ฉ - ๋ง์
๋ฌธ์์ด์ ์
๋ ฅ๋ฐ์ ๋ง์
์คํ
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "๋ง์
๋ฌธ์์ด ์
๋ ฅํ๊ธฐ" << endl;
getline(cin, s, '\n');
int sum = 0;
// ๋ฌธ์์ด ๋ด์ ๊ฒ์ํ ์์ ์ธ๋ฑ์ค
int startIndex = 0;
while (true)
{
// "+" ๋ฌธ์ ๊ฒ์
int fIndex = s.find('+', startIndex);
// "+" ๋ฌธ์ ๋ฐ๊ฒฌํ ์ ์์
if (fIndex == -1)
{
string part = s.substr(startIndex);
// "+" ์ผ๋ก ๋๋๋ ๊ฒฝ์ฐ
if (part == "")
break;
cout << part << endl;
// ๋ฌธ์์ด์ ์๋ก ๋ณํํ์ฌ ๋ํ๊ธฐ
sum += stoi(part);
break;
}
// ์๋ธ์คํธ๋ง์ผ๋ก ์๋ฅผ ๋ฌธ์ ๊ฐ์
int count = fIndex - startIndex;
string part = s.substr(startIndex, count);
cout << part << endl;
// ๋ฌธ์์ด์ ์๋ก ๋ณํํ์ฌ ๋ํ๊ธฐ
sum += stoi(part);
// ๊ฒ์์ ์์ํ ์ธ๋ฑ์ค ์ ์ง์ํด
startIndex = fIndex + 1;
}
cout << "์ซ์๋ค์ ํฉ์ " << sum;
}
/*
๋ง์
๋ฌธ์์ด ์
๋ ฅํ๊ธฐ
66+4+24+3+25
66
4
24
3
25
์ซ์๋ค์ ํฉ์ 122
*/
// ๋ฌธ์์ด find ๋ฐ replace
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "์ฌ๋ฌ ์ค์ ๋ฌธ์์ด ์
๋ ฅํ์ธ์. ์
๋ ฅ์ ๋์ & ๋ฌธ์" << endl;
getline(cin, s, '&');
// enter ํค ์ ๊ฑฐํ๊ธฐ ์ํ ์ฝ๋
cin.ignore();
string f, r;
cout << endl
<< "find : ";
getline(cin, f, '\n'); // ๊ฒ์ํ ๋ฌธ์์ด ์
๋ ฅ
cout << "replace : ";
getline(cin, r, '\n'); // ๋์นํ ๋ฌธ์์ด ์
๋ ฅ
int startIndex = 0;
while (true)
{
int fIndex = s.find(f, startIndex);
if (fIndex == -1)
{
break;
}
// fIndex ๋ถํฐ ๋ฌธ์์ด f์ ๊ธธ์ด๋งํผ ๋ฌธ์์ด r๋ก ๋ณ๊ฒฝ
s.replace(fIndex, f.length(), r);
startIndex = fIndex + r.length();
}
cout << s << endl;
}
/*
์ฌ๋ฌ ์ค์ ๋ฌธ์์ด ์
๋ ฅํ์ธ์. ์
๋ ฅ์ ๋์ & ๋ฌธ์
hi, today is thursday! I am very hungry. And I will sleep now. say hello! hi girl~ we need to go to bed right now~ see sddsson &
find : now
replace : nooooooooow
hi, today is thursday! I am very hungry. And I will sleep nooooooooow. say hello! hi girl~ we need to go to bed right nooooooooow~ sddsson*/