从两个列之一返回值,或跳过数组中的所有其他元素
Return values from one of two columns, or skip every other element in an array?
我正在尝试编写两个单独的函数,这两个功能都从数据文件中读取,但只返回两个列中的一个。(评论不在.dat文件中,只是为了澄清而写)
// Hours Pay Rate
40.0 10.00
38.5 9.50
16.0 7.50
42.5 8.25
22.5 9.50
40.0 8.00
38.0 8.00
40.0 9.00
44.0 11.75
如何返回一个函数中代表"小时"的元素,然后在另一个功能中返回"付费率"?
使用 fstream 或 ifstream 对象和提取操作员。
std::ifstream fin(YourFilenameHere);
double hours, rate;
fin >> hours >> rate;
这些对象的类位于fstream标题中。
// "hours" and "payRate" might as well be class members, depending
// on your design.
vector<float> hours;
vector<float> payRate;
std::ifstream in(fileName.c_str());
string line;
while (std::getline(in, line)) {
// Assuming they are separated in the file by a tab, this is not clear from your question.
size_t indexOfTab = line.find('t');
hours.push_back(atof(line.substr(0. indexOfTab).c_str());
payRate.push_back(atof(line.substr(indexOfTab +1).c_str()));
}
现在,您可以在数小时乘数小时访问I'th进入[I],同样的薪金率。同样,您可以通过返回相应的向量来"返回一列",如果您确实需要。
相关文章:
- 数组元素打印的递归方法
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 数组元素更改值?
- 如何访问宏中定义的数组元素
- 将数组元素递增 1
- C++函数,它将数组、谓词和运算符作为参数,并将运算符应用于满足谓词的数组元素
- 存储指向动态数组元素的指针
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- 在 std::map 中插入数组元素
- 使用指针访问数组元素时出现意外结果
- c++ 使用动态分配运算符反向数组元素
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- 缺少数组元素问题
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 创建一个函数来转换数组元素的类型并返回数组的地址
- 删除在结构 c++ 中声明的数组元素
- Getter 和 Setter 用于类 C++ 中的数组元素
- 尝试递归获取数组元素的总和
- 使用SWIG将numpy数组元素(int)传递给c++int