1. string -> int 바꾸기

#include <iostream>
#include <string>

int main(){
	int num = stoi("122");
}

 

2. sstream 사용해 특정 문자열로 구분하여 자르기

#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main() {
    string fruit = "apple lemon grape melon";
    istringstream ss(fruit);
    vector <string> words;
    string word;

    //words에는 apple, lemon, grape, melon이 저장됨
    while (getline(ss, word, ' ')) //공백 단위로 구분
        words.push_back(word);
}

'⚖️Algorithm > ⚙️C++' 카테고리의 다른 글

[c++] vector  (0) 2022.05.01

+ Recent posts