Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何用c++;?_C++_Data Structures - Fatal编程技术网

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;
}