C++ 복습용 정리 [<string>, io, <algorithm>]

jjin·2023년 11월 2일
0

기본 입출력

정수 <-> 문자열

to_string(n)
stoi(s)

정수 <-> 자리수 쪼갠 배열

vector<int> digits;
string s = to_string(n);

for (char c : s) {
	digits.push_back(c - '0');
}
return digits;
int n = 0;

for (int d : digits) {
	n = n * 10 + d;
}

n 개 공백 구분 정수 담기

vector<int> nums(n);

for (int i = 0; i < n; i++) {
	cin >> nums[i]
}

몇 개인지 모를 때

istringstream: 공백 구분 정수 담기

getline(cin, line);
istringstream iss(line);

int num;
while (iss >> num) {
	nums.push_back(num);
}

while(cin >> n): 줄바꿈 구분 정수 담기

while (cin >> num) {
	nums.push_back(num);
}

while(getline(cin, line)): EOF 전까지 줄바꿈 구분 문자열 담기

while(getline(cin, line)) {
	lines.push_back(line);
}

String 클래스 함수

생성자


	string s;
    assert(s.empty() && s.length() == 0 && s.size() == 0);


	string s(4, "a"); // aaaa
    
	
    string s("abcdef", 3); // abc


	string const other("hello");
	string s(other, 0, 4); // hell


	string s(string("a") + string("b")) // ab


	char mutableCstr[] = "abcdef";
	string s(begin(mutableCstr) + 2, end(mutableCstr) - 1); // cde

	

substr

basic_string substr(size_type pos=0, size_type count=npos) const

find

algorithm 헤더 함수

find

template <class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& val);
profile
진짜

0개의 댓글