C++ 如何用c++;?
我们可以使用C++ 如何用c++;?,c++,data-structures,C++,Data Structures,我们可以使用structure.element打印结构的元素。但我想马上打印一个完整的结构 是否有类似于cout的方法。你应该重写你需要重载操作符重载你想知道的一切:你必须重载std::ostream&operator重载可能重复的 struct node { int next; string data; }; main() { node n; cout<<n; } friend ostream& operator<< (ostream &am
structure.element
打印结构的元素。但我想马上打印一个完整的结构
是否有类似于
cout的方法。你应该重写你需要重载操作符重载你想知道的一切:你必须重载std::ostream&operator重载可能重复的
struct node {
int next;
string data;
};
main()
{
node n;
cout<<n;
}
friend ostream& operator<< (ostream & in, const node& n){
in << "(" << n.next << "," << n.data << ")" << endl;
return in;
}
#include <string>
#include <iostream>
struct node {
int next;
std::string data;
friend std::ostream& operator<< (std::ostream& stream, const node& myNode) {
stream << "next: " << myNode.next << ", Data: " << myNode.data << std::endl;
return stream;
}
};
int main(int argc, char** argv) {
node n{1, "Hi"};
std::cout << n << std::endl;
return 0;
}