ifstream istream和函数调用
ifstream-istream and function call
我是c++的新手,在处理流时,我看到了以下代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
class Results
{
public:
string const& operator[](int index) const
{
return m_data[index];
}
int size() const
{
return m_data.size();
}
void readnext(istream& str)
{
string line;
getline(str, line);
cout << "line :" << line <<endl;
stringstream lineStream(line);
string cell;
m_data.clear();
while(getline(lineStream, cell, ','))
{
m_data.push_back(cell);
cout << cell<<endl;
}
}
private:
vector<string> m_data;
};
istream& operator>>(istream& str, Results & data)
{
data.readnext(str);
return str;
}
int main()
{
ifstream file("filename.txt");
Results r1;
while(file >> r1)
{
cout << "1st element: " << r1[3] << "n";
}
}
当呼叫data.readnext(str)
时:1( 作为参数传递的str
的值是多少?当我打印出来时,我得到0x7ffd30f01b10,这是一个地址。2( 在函数CCD_ 3中,将文件的第一行的值赋予行。我不明白为什么。那不应该是getline(file, line);
吗我通常不理解这是如何工作的,因此任何帮助都将不胜感激
-
该值是对实例化的
std::ifstream file
对象的std::istream
超类的引用。 -
没有。在
readnext()
函数的作用域中没有可见的file
对象。代码正确。str
是readnext()
的std::istream &
参数,并且将第一个参数的类型与std::getline()
匹配。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- ifstream istream和函数调用
- 需要有关此编译错误的帮助 getline 函数错误:没有匹配函数调用"getline(std::istream&,char&)"|