C++ c++;添加逗号分隔的值

C++ c++;添加逗号分隔的值,c++,csv,stringstream,atoi,C++,Csv,Stringstream,Atoi,试图从字符串中添加一些逗号分隔的值。我觉得我需要删除逗号。这是stringstream的情况吗 string str = "4, 3, 2" //Get individual numbers //Add them together //output the sum. Prints 9 我会在while循环中使用istringstream和getline来拆分(标记化)逗号周围的字符串。 然后只需使用std::stoi将每个字符串标记转换为一个整数,并将该数字添加到总和中std::stoi丢弃字

试图从字符串中添加一些逗号分隔的值。我觉得我需要删除逗号。这是stringstream的情况吗

string str = "4, 3, 2"
//Get individual numbers
//Add them together
//output the sum. Prints 9

我会在while循环中使用
istringstream
getline
来拆分(标记化)逗号周围的字符串。 然后只需使用
std::stoi
将每个字符串标记转换为一个整数,并将该数字添加到总和中
std::stoi
丢弃字符串输入中的任何空白字符

std::string str = "4, 3, 2";
std::istringstream ss(str);

int sum = 0;
std::string token;
while(std::getline(ss, token, ',')) {
    sum += std::stoi(token);
}
std::cout << "The sum: " << sum;
std::string str=“4,3,2”;
std::istringstream ss(str);
整数和=0;
字符串标记;
while(std::getline(ss,token,,')){
sum+=std::stoi(令牌);
}

你说得对吗。一种解决方案是std::istringstream与std::getlineWelcome to Stack Overflow结合使用。你试过什么?