3번 보기!

     

    1. c++에서 str = char* ; 구문은 사용 가능하나,

    char*에 str을 넣는 것은 불가능하다.

     

    2. strtok를 사용해서 자르기 

    #define _CRT_SECURE_NO_WARNINGS 
    
    #include <iostream>
    #include <vector>
    #include <sstream>
    
    
    using namespace std;
    
    
    int main() {
    	//공백 포함 입력
    	char input[100];
    	cin.getline(input, 100, '\n');
    
    	cout << input;
    
    	vector<string> save_str;
    
    	//", " 을 찾아서 null을 넣고, 한번 자름.
    	//그리고 이를 잘랐다는 사실을 기억해둠
    	//strtok는 반환형이 char* 임을 명심.
    	char* splitStr = strtok(input, ", ");
    
    	while (splitStr != NULL) {
    		save_str.push_back(splitStr);
    		cout << splitStr << endl;
    
    		//마지막에 null이 들어감을 주의!
    		splitStr = strtok(NULL, ", ");
    	}
    	
    	for (int i = 0; i < save_str.size(); i++) {
    		cout << save_str[i] <<endl;
    	}
    
    	return 0;
    }

     

    3.

    getline 은 cin버전과 ss버전이 있다.

     

    cin버전은 단순히 공백 포함해서 문자를 받고 싶을 때 사용하고, ss버전은 특정 char기준으로 나누고 싶을 때 사용한다.

     

    <cin버전>

    getline(cin, str, '\n');

     

    <ss버전>

    1. sstream추가

    2. 자르려는 문자열을 ss버전으로 변경

    3. 자른 문자열을 임시 저장하는 buffer선언

    4. 반복문에 넣어서 문자 끝까지 자르기 반복(반복할때마다 buffer에 저장되는 값을 따로 옮기기)

    #include<sstream>//1. 추가
    
    vector<string> strVector(4);
    
    string str = "09:17 19:24"
    
    istringstream ss(str);//2. ss선언
    
    string buffer1;//3. buffer선언
    string buffer2;
    
    //09:17 과 19:24로 분리
    while (getline(ss, buffer1, ' ')) {//4. 반복문으로 분리
        istringstream ss2(buffer1);
    
        //09 와 17, 19와 24로 분리
        while (getline(ss2, buffer2, ':')) {
            strVector[index] = buffer2;
            index++;
        }
    }

     

     

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    
    using namespace std;
    
    
    int main() {
    	
    	ios::sync_with_stdio(0);
    	vector<string> str(7);
    
    	string input;
    	cin >> input;
    
    	istringstream ss(input);
    	string buffer;
    	vector<string> params;
    
    	while (getline(ss, buffer, ','))
    		params.push_back(buffer);
    
    	
    	return 0;
    }
    • 네이버 블러그 공유하기
    • 네이버 밴드에 공유하기
    • 페이스북 공유하기
    • 카카오스토리 공유하기