c++中添加逗号分隔值
c++ adding comma separated values
尝试从字符串中添加一些逗号分隔的值。我觉得我需要去掉逗号。这是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;
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- c++中添加逗号分隔值