我想从字符串中的值执行数学运算
I want to perform a mathematical operation from values in a string
string resistance;
ifstream in;
in.open(filename);
while (!in.eof())
{
getline(in, resistance);
cout << resistance <<endl;
}
in.close();
这是我用来从文件中读取的代码。该文件的输出为:例如:
1025 10 60 45 35
10 45 23 45 65 88
我想取每行,并通过行的每个值对变量执行数学运算
首先,拆分字符串,然后使用 atoi
将它们转换为数字,然后遍历那些正在执行操作的值。
在 C++11 中,您可以使用 std::stoi 函数:
istringstream iss( resistance);
istream_iterator<string> it = iss.begin();
while( it != iss.end()) {
int value = stoi( *it++);
//... do something
}
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- GCC本机矩阵运算库
- 位阵列上的快速AND运算
- 字符串中int的加法运算
- 算术运算的结果类似于:C浮点变量中的1/3
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- 如何在 vtk Poly 数据上进行布尔运算?
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 避免指针运算,修复叮当整齐错误
- 使用双精度的浮点运算
- 如何在 c++ 中使用带有数学运算的引用/指针?
- 更快的C++算术运算
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 两个字符串之间的数学运算
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 何时使用按位运算而不是算术替代方法?
- Arduino 上数学运算的计时速度 - 异常
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度