string값을 공백 기준으로 자르고 각각을 class 멤버 변수로 사용하기

김기현·2023년 3월 13일
0

Vector를 입력받아 각각의 벡터 값들을 stringstream을 이용하여 공백을 기준으로 자르고 자른 값들로 Car클라스 객체의 멤버 변수를 만드는데 사용하였다.

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

using namespace std;

class Car
{
private:
    string time;
    string carNum;
    string inNOut;
public:
    Car(string theTime,string theCarNum,string theInNOut)
        :time(theTime),carNum(theCarNum),inNOut(theInNOut)
        {}
};

Car* carArr[10000];

/*
입력값은:
["05:31 1161 IN", "06:01 0000 IN", "06:31 0000 OUT", "07:01 1161 OUT", "07:49 0148 IN", "17:59 0000 IN", "18:09 0148 OUT", "21:59 1161 IN", "23:00 1161 OUT"]
*/

void solution(vector<int> fees, vector<string> records) {
    int num=0;
    for (auto& record : records) {
        stringstream ss(record);
        string a, b, c;
        ss >> a >> b >> c;
        carArr[num] = new Car(a,b,c);
    }
}

profile
김기현입니다

0개의 댓글