C++ 将输入流转换为双向量

C++ 将输入流转换为双向量,c++,visual-studio,vector,stringstream,C++,Visual Studio,Vector,Stringstream,我在获取逗号分隔数字的输入行以正确地传递到双向量时遇到问题 我对C++有点新的看法,我有点不习惯。我尝试过使用双数组,但双向量似乎效果更好 int main(){ vector<double> vect; string input1; string token; int i; int size; cout << "Please input up to 1000 comma-delimited numbers (I.E. '5

我在获取逗号分隔数字的输入行以正确地传递到双向量时遇到问题

我对C++有点新的看法,我有点不习惯。我尝试过使用双数组,但双向量似乎效果更好

int main(){
    vector<double> vect;
    string input1;
    string token;
    int i;
    int size;
    cout << "Please input up to 1000 comma-delimited numbers (I.E. '5,4,7.2,5'): ";
    cin >> input1;
    stringstream ss(input1);


    while (ss >> i){
        vect.push_back(i);
        if (ss.peek() == ','){
            ss.ignore();
        }
    }

    for (int j = 0; j < vect.size(); j++){
        cout << vect.at(j) << ", ";
    }

}
intmain(){
向量向量;
字符串输入1;
字符串标记;
int i;
整数大小;
cout>input1;
字符串流ss(输入1);
while(ss>>i){
向量推回(i);
如果(ss.peek()==','){
忽略();
}
}
对于(int j=0;jcout您正在使用整数从
ss
inti;
)读取数据。整数不能包含小数点或分数。将其更改为
double
,您就可以了。而且
std::vector
几乎总是优于普通数组

请注意,在上一个for循环中,还可以使用下标运算符访问向量元素:

for (int j = 0; j < vect.size(); j++){
    cout << vect[j] << ", ";
}
for(int j=0;j太感谢你了!我不敢相信我忽略了这一点。我有一个习惯,就是漏掉一个小细节,然后炸掉我的项目。另外,流->字符串->字符串流->解析是不必要的低效