본문 바로가기

PROGRAM/C++

CH5. 문자열 분할

- 문자열은 특별한 종류의 컨테이너라고 생각할 수 있음

- 몇가지 컨테이너 연산을 지원

- 인덱스를 사용할 수 있고 벡터처럼 반복자를 제공 -> 벡터에 적용할 수 있는 다양한 연산을 문자열에 적용 가능

// 자동으로 include alt + Enter or ctrl + .(dot)

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


vector<string> split(const string& s);

int main() {
	string s;
	// 문자열을 한 행씩 입력받아 분할
	while (getline(cin, s)) {
		cout << endl << "output : " << endl;
		vector<string> v = split(s);
		// 벡터 v에 저장한 단어를 각각 출력
		for (vector<string>::size_type i = 0; i != v.size(); ++i)
			cout << v[i] << endl;
	}
	return 0;
}


vector<string> split(const string& s)
{
	vector<string> ret;
	typedef string::size_type string_size;
	string_size i = 0;
	// invariant: 지금까지  [원래 i, 현재 i)범위의 문자들을 처리
	while (i != s.size()) {
		// invariant: [원래 i, 현재 i)범위에 있는 문자들은 모두 공백
		while (i != s.size() && isspace(s[i]))
			i++;
		// 순서상 다음 단어의 끝을 찾음
		string_size j = i;
		// invariant: [원래 j 현재 j)범위에 있는 문자들은 공백이 아님
		while (j != s.size() && !isspace(s[j]))
			j++;
		// 공백이 아닌 문자들을 찾았을때
		if (i != j) {
			// i에서 부터 j - i의 문자들을 s에 복사
			ret.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return ret;
}

위의 코드와 아래의 제공된 함수는 같은 역할을 한다.

int main()
{
	string s;
    while (cin >> s)
		cout << s << endl;
        
	return 0;
}

 

 

입력된 문자열의 최대 길이구하기

string::size_type width(const vector<string>& v)
{
	string::size_type maxlen = 0;

	for (vector<string>::size_type i = 0; i != v.size(); ++i)
		maxlen = max(maxlen, v[i].size());

	return maxlen;
}

프레임으로 테두리 추가하기

vector<string> frame(const vector<string>& v) {
	vector<string> ret;
	string::size_type maxlen = width(v);
	string border(maxlen + 4, '*');
	// write the top border
	ret.push_back(border);
	// write each interior row, bordered by an asterisk and a space
	for (vector<string>::size_type i = 0; i != v.size(); ++i) {
		ret.push_back("* " + v[i] +
			string(maxlen - v[i].size(), ' ') + " *");
	}
	// write the bottom border
	ret.push_back(border);
	return ret;
}

 

-합쳐진 코드

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;


vector<string> split(const string& s);
string::size_type width(const vector<string>& v);
vector<string> frame(const vector<string>& v);


int main() {
	string s;
	// 문자열을 한 행씩 입력받아 분할
	while (getline(cin, s)) {
		cout << endl << "output : " << endl;
		vector<string> v = split(s);
		v = frame(v);
		// 벡터 v에 저장한 단어를 각각 출력
		for (vector<string>::size_type i = 0; i != v.size(); ++i)
			cout << v[i] << endl;
	}
	return 0;
}


vector<string> split(const string& s)
{
	vector<string> ret;
	typedef string::size_type string_size;
	string_size i = 0;
	// invariant: 지금까지  [원래 i, 현재 i)범위의 문자들을 처리
	while (i != s.size()) {
		// invariant: [원래 i, 현재 i)범위에 있는 문자들은 모두 공백
		while (i != s.size() && isspace(s[i]))
			i++;
		// 순서상 다음 단어의 끝을 찾음
		string_size j = i;
		// invariant: [원래 j 현재 j)범위에 있는 문자들은 공백이 아님
		while (j != s.size() && !isspace(s[j]))
			j++;
		// 공백이 아닌 문자들을 찾았을때
		if (i != j) {
			// i에서 부터 j - i의 문자들을 s에 복사
			ret.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return ret;
}
string::size_type width(const vector<string>& v)
{
	string::size_type maxlen = 0;

	for (vector<string>::size_type i = 0; i != v.size(); ++i)
		maxlen = max(maxlen, v[i].size());

	return maxlen;
}


vector<string> frame(const vector<string>& v) {
	vector<string> ret;
	string::size_type maxlen = width(v);
	string border(maxlen + 4, '*');
	// write the top border
	ret.push_back(border);
	// write each interior row, bordered by an asterisk and a space
	for (vector<string>::size_type i = 0; i != v.size(); ++i) {
		ret.push_back("* " + v[i] +
			string(maxlen - v[i].size(), ' ') + " *");
	}
	// write the bottom border
	ret.push_back(border);
	return ret;
}

 

'PROGRAM > C++' 카테고리의 다른 글

Visual Studio 2019 community Secure Warning 무시하기  (0) 2021.03.09
열혈C++ CH06  (0) 2021.03.09
4. 프로그램 및 데이터 구조화 -2  (0) 2020.08.18
CH04. 프로그램 및 데이터 구조화  (0) 2020.08.17
CH03. 데이터 일괄처리  (0) 2020.08.17