728x90
๐ C++ ๋ฌธ์์ด ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ
1. cout์ ์ด์ฉํ ์ถ๋ ฅ (๊ถ์ฅ)
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
cout << str << endl;
}
โ ์ฅ์
- std::string์ ์ง์ ์ถ๋ ฅ ๊ฐ๋ฅ → ๋ณํ ๊ณผ์ ํ์ ์์
- ๊ฐ๋ ์ฑ์ด ์ข๊ณ , C++ ์คํ์ผ์ ๋ง๋ ๋ฐฉ์
2. printf๋ฅผ ์ด์ฉํ ์ถ๋ ฅ
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
printf("%s\n", str.c_str()); // c_str()์ ์ฌ์ฉํ์ฌ ๋ณํ
}
โ ์ฃผ์ํ ์
- std::string์ ์ง์ ์ฌ์ฉํ ์ ์์ → .c_str()์ ์ฌ์ฉํ์ฌ const char*๋ก ๋ณํํด์ผ ํจ
- printf๋ C ์คํ์ผ์ ์ถ๋ ฅ ํจ์๋ผ์ C++์์๋ ์ ์ฌ์ฉ๋์ง ์์
- cout๋ณด๋ค ํฌ๋งท ์ ์ด๊ฐ ํธ๋ฆฌํ์ง๋ง, ํ์ ์์ ์ฑ์ด ๋จ์ด์ง
3. puts๋ฅผ ์ด์ฉํ ์ถ๋ ฅ (C ์คํ์ผ)
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
puts(str.c_str()); // c_str()์ ์ฌ์ฉํ์ฌ ๋ณํ
}
โ ํน์ง
- puts()๋ ๋ฌธ์์ด์ ์ถ๋ ฅํ๊ณ ์๋์ผ๋ก ๊ฐํ์ ์ถ๊ฐํจ (\n ๋ถํ์)
- printf("%s\n", str.c_str());๊ณผ ๋์ผํ ํจ๊ณผ
4. ๋ฌธ์์ด ๋ฐ๋ณต ์ถ๋ ฅ (for ๋ฃจํ)
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
for (char ch : str) {
cout << ch << " ";
}
cout << endl;
}
โ ํน์ง
- std::string์ char์ ๋ฐฐ์ด์ด๋ฏ๋ก for ๋ฃจํ๋ฅผ ์ฌ์ฉํด ํ ๊ธ์์ฉ ์ถ๋ ฅ ๊ฐ๋ฅ
- ๊ฐ ๋ฌธ์๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ์ฒ๋ฆฌํ ๋ ์ ์ฉ
5. ํ์ผ๋ก ์ถ๋ ฅ (ofstream)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "Hello, File!";
ofstream outFile("output.txt"); // ํ์ผ ์ด๊ธฐ
outFile << str << endl; // ํ์ผ์ ๋ฌธ์์ด ์ถ๋ ฅ
outFile.close(); // ํ์ผ ๋ซ๊ธฐ
}
โ ํน์ง
- ํ์ผ์ ๋ฌธ์์ด์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ
- ofstream ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ๋ฌธ์์ด์ ์ถ๋ ฅํ ์ ์์
6. C ์คํ์ผ์ char* ๋ฌธ์์ด ์ถ๋ ฅ
#include <iostream>
using namespace std;
int main() {
const char* str = "Hello, C-style!";
cout << str << endl;
}
โ ํน์ง
- std::string์ด ์๋๋ผ char* ํฌ์ธํฐ๋ฅผ ์ฌ์ฉ
- ํ๋ C++์์๋ std::string์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋จ
๐ก ์ ๋ฆฌ
๋ฐฉ๋ฒ | ์ค๋ช | ๋ณํ ํ์ ์ฌ๋ถ | ๊ฐํ ์๋ ์ถ๊ฐ | ์ถ์ฒ ์ฌ๋ถ |
cout | C++ ์คํ์ผ ์ถ๋ ฅ ๋ฐฉ์ | โ | โ (endl ํ์) | โ (๊ฐ์ฅ ์ถ์ฒ) |
printf | C ์คํ์ผ ์ถ๋ ฅ ๋ฐฉ์ | โ (.c_str()) | โ (\n ํ์) | โ (ํน์ ์ํฉ์์๋ง) |
puts | ๊ฐ๋จํ ๋ฌธ์์ด ์ถ๋ ฅ | โ (.c_str()) | โ | โญ (๊ฐ๋จํ ๊ฒฝ์ฐ) |
for ๋ฃจํ | ๋ฌธ์์ด ๋ฌธ์๋ณ ์ถ๋ ฅ | โ | โ | โญ (๋ฌธ์ ๋จ์ ์ฒ๋ฆฌ ํ์ ์) |
ofstream | ํ์ผ๋ก ๋ฌธ์์ด ์ถ๋ ฅ | โ | โ (endl ํ์) | โญ (ํ์ผ ์ถ๋ ฅ ํ์ ์) |
char* | C ์คํ์ผ ๋ฌธ์์ด ์ถ๋ ฅ | โ | โ (endl ํ์) | โ (ํ๋ C++์์๋ ๋น์ถ์ฒ) |
๐ก ์ผ๋ฐ์ ์ผ๋ก std::cout์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข๊ณ , ํ์ผ ์ถ๋ ฅ์ด ํ์ํ๋ฉด ofstream์ ์ฌ์ฉํ๋ฉด ๋จ! ๐
โ C++์์ std::string์ printf๋ก ์ถ๋ ฅํ๋ ค๊ณ ํ๋ฉด ๋ฐ์ํ๋ "std::string"์์ "const char *"๋ก์ ์ ์ ํ ๋ณํ ํจ์๊ฐ ์์ต๋๋ค. ์ค๋ฅ ํด๊ฒฐ๋ฐฉ๋ฒ
๐ ์์ธ :
- printf ํจ์๋ const char* ํ์์ ๋ฌธ์์ด์ ๊ธฐ๋ํ์ง๋ง, std::string์ ์ด๋ฅผ ์๋ ๋ณํํ์ง ์์.
- ๋ฐ๋ผ์ std::string์ printf๋ก ์ง์ ์ถ๋ ฅํ๋ฉด ์ปดํ์ผ ์ค๋ฅ ๋ฐ์.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ:
- c_str() ์ฌ์ฉํ์ฌ const char*๋ก ๋ณํ
- printf("%s", solution11(3).c_str());
- cout์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ (๊ถ์ฅ)
- cout์ std::string์ ์๋์ผ๋ก ์ฒ๋ฆฌ ๊ฐ๋ฅํ๋ฏ๋ก ๋ ๊ฐ๋จํ๊ณ ๊ฐ๋ ์ฑ์ด ์ข์.
- cout << solution11(3) << endl;
โ ์ต์ข ์ฝ๋ ์์ ์์:
#include <iostream>
#include <string>
using namespace std;
string solution11(int num)
{
return num % 2 ? "Odd" : "Even";
}
int main()
{
cout << solution11(3) << endl; // ๊ถ์ฅ ๋ฐฉ๋ฒ
// printf("%s", solution11(3).c_str()); // printf๋ฅผ ๊ผญ ์จ์ผ ํ๋ค๋ฉด ์ด๋ ๊ฒ
}
์ฐธ๊ณ
'C++ > C++ ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
c++ int๋ฅผ ๋ฌธ์๋ฐฐ์ด๋ก ๋ง๋ค๊ธฐ (0) | 2025.04.11 |
---|---|
for๋ฌธ (0) | 2025.03.29 |
sort() ํจ์ ์ฌ์ฉ๋ฒ (0) | 2025.03.23 |