[C++] ๋ฌธ์ž์—ด ์ŠคํŠธ๋ฆผ(stringstream)

2022. 4. 23. 16:25ยท๐Ÿ“ Language/โœ C & C++

๋ฌธ์ž์—ด ์ŠคํŠธ๋ฆผ

๋ฌธ์ž์—ด์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž๋ฃŒํ˜•์ด ๋“ค์–ด์™”์„ ๋•Œ ์šฉ๋„์— ๋งž๊ฒŒ ํŒŒ์‹ฑํ•˜๊ธฐ ์œ ์šฉํ•˜๋‹ค.

  • ํ—ค๋” #include <sstream>
์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ ๋ฌธ์ž์—ด์„ ํ’€์ดํ•  ๋•Œ C++ string๋งŒ์œผ๋กœ ํ•ด๊ฒฐํ•˜๊ธฐ์—” ์ž‘์€ ์–ด๋ ค์›€์ด ์žˆ๋‹ค.

 

1. ๋ฌธ์ž์—ด ์ŠคํŠธ๋ฆผ

1) stringstream

  • ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ: ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ, ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋ชจ๋‘ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2) istringstream

  • ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ
  • ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ๊ณผ '\n'์„ ๊ธฐ์ค€์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ๋ถ„๋ฆฌํ•  ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰ ์‹œ ์ž๋ฃŒํ˜•์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ์‹คํ–‰๋œ๋‹ค.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
	istringstream iss("test\n123 aaa 456");
	string s1, s2;
	int i1, i2;
	iss >> s1 >> i1 >> s2 >> i2; // ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑํ•˜๊ณ  ๋ณ€์ˆ˜ํ˜•์— ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•œ๋‹ค.

	cout << s1 << endl; // test
	cout << i1 << endl; // 123
	cout << s2 << endl; // aaa
	cout << i2 << endl; // 456
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
 int main() {
    string str1 = "1D2S#10S";
    string str2 = "1111DAWV2S#10S";
     
    istringstream iss1(str1);
    istringstream iss2(str2);
    int num1, num2;
    while (iss1 >> num1) cout << num1 << " ";
    cout << endl;
    while (iss2 >> num2) cout << num2 << " ";
    cout << endl;
    
    istringstream iss3(str1);
    istringstream iss4(str2);
    char ch1, ch2;
    while (iss3 >> ch1) cout << ch1 << " ";
    cout << endl;
    while (iss4 >> ch2) cout << ch2 << " ";
    cout << endl;
}

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// 1 
// 1111 
// 1 D 2 S # 1 0 S 
// 1 1 1 1 D A W V 2 S # 1 0 S

 

3) ostringstream

  • ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ
  • ๋ฌธ์ž์—ด์„ ์กฐ๋ฆฝํ•˜๊ฑฐ๋‚˜ ํŠน์ • ํ˜•์‹์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
	ostringstream oss;
	string s1 = "abc", s2 = "gjw";
	int i1 = 19234;
	double d1 = 3.591;
	oss << s1 << "\n" << i1 << "\n" << s2 << "\n" << d1; // ๋ฌธ์ž์—ด์„ ๋ถ™์ธ๋‹ค.
	cout << oss.str(); // ๋ฌธ์ž์—ด์„ ๊บผ๋‚ธ๋‹ค.
}

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// abc
// 19234
// gjw
// 3.591

 

4) str(), clear()

  • str(string s): stringstream์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ”๊พผ๋‹ค. ์ด๋•Œ s๊ฐ€ "" ์ผ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.
  • str(): stringstream์ด ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์˜ ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • clear(): stringstream ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด clear()๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ ์ €์žฅ๋œ ๋ฌธ์ž์—ด์ด ์‚ญ์ œ๋˜์ง„ ์•Š๋Š”๋‹ค.

 

5) get(), unget()

  • get(): ์ปค์„œ๋ฅผ ๋’ค๋กœ ์˜ฎ๊ธฐ๋ฉด์„œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • unget(): ์ปค์„œ๋ฅผ ์•ž์œผ๋กœ ๋‹ค์‹œ ์˜ฎ๊ธด๋‹ค.
string str = "123abc";

// get()
stringstream ss1;
ss1.str(str);
cout << ss1.get() - '0'; // 1: -'0' ์•ˆํ•ด์ฃผ๋ฉด ์•„์Šคํ‚ค์ฝ”๋“œ๊ฐ’์ด ๋‚˜์˜ด
cout << ss1.get() - '0'; // 2

// unget()
stringstream ss2;
ss2.str(str);
char ch;
ss2 >> ch; // 1
ss2 >> ch; // 2
ss2.unget();
ss2 >> ch; // 1

 

6) getline()

  • ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์ด๋‚˜ '\n'์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
	string str = "gkg|qiew|789", token;
	stringstream ss(str);
	while (getline(ss, token, '|')) {
		cout << token << endl;
	}
}

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// gkg
// qiew
// 789

 

2. ํ™œ์šฉ

1) ๋‚ ์งœ๋ฅผ ์ดˆ๋กœ ๋ฐ”๊พธ๊ธฐ

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(void) {
    vector<long long> time;
    string str = "2019:06:30 12:00:30"; // ์—ฐ ์›” ์ผ ์‹œ ๋ถ„ ์ดˆ 
    for (int i = 0; i < str.size(); i++)
        if (str[i] == ':') str[i] = ' ';
    
    long long num = 0;
    stringstream stream;
    stream.str(str);
    while (stream >> num)
        time.push_back(num);
    
    long long second = 0;
    second += time[0] * 365 * 24 * 60 * 60; // ์—ฐ 
    second += time[1] * 30 * 24 * 60 * 60; // ์›”
    second += time[2] * 24 * 60 * 60; // ์ผ
    second += time[3] * 60 * 60; // ์‹œ 
    second += time[4] * 60; // ๋ถ„ 
    second += time[5]; // ์ดˆ 
    cout << second;
}
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Language/โœ C & C++' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] RTTI(Run Time Type Information)
  • [C++] ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ(Operator Overloading)
  • [C++] l-value์™€ r-value
  • [C/C++] malloc()๊ณผ new์˜ ์ฐจ์ด
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ๋ฌธ์ž์—ด ์ŠคํŠธ๋ฆผ(stringstream)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”