vector push_back 的 C++ 问题

标签 c++ class stl dictionary

更新: 下面的代码给我一个错误

Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const Graph&)': Graph.cpp:43: error: passing 'const std::map >, std::less, std::allocator > > > >' as 'this' argument of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare = std::less, _Alloc = std::allocator > > >]' discards qualifiers Graph.cpp:44: error: passing 'const std::map >, std::less, std::allocator > > > >' as 'this' argument of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare = std::less, _Alloc = std::allocator > > >]' discards qualifiers make[2]: * [build/Debug/GNU-MacOSX/Graph.o] Error 1

class Graph {
public:
    Graph();
    Graph(const Graph& orig);
    virtual ~Graph();

    void clear();
    void rgg(lint, double);
    bool is_directed() { return directed; }
    friend ostream& operator<< (ostream&, const Graph&); 


private:
    map< lint, vector<lint> > adjList;
    vector< pair<double, double> > xy;
    double radius;
    MTRand Rand;
    bool directed;
};


void Graph::rgg(lint n, double r) {
    radius = r; directed = false;

    clear(); 

    for(lint i = 0; i < n; i++)
        xy.push_back(pair<double, double>(Rand.rand(), Rand.rand()));
}

ostream& operator<< (ostream& os, const Graph& inGraph) {
    for(lint i = 0; i < inGraph.nodes; i++) {
        os << i << " ";
        if( inGraph.adjList.find(i) != inGraph.adjList.end() ) {
            for(lint idx = 0; idx < (inGraph.adjList[i]).size(); idx++ )
                os << inGraph.adjList[i].at(idx) << " ";

        }
        os << endl;
    }
}

提前谢谢你,

最佳答案

我怀疑你指的是 Rand 而不是 MTRand:

Rand.rand()

MTRand 是类型的名称。 Rand 是您创建的实例的名称。

关于 vector push_back 的 C++ 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4698852/

相关文章:

c++ - 如何在编译时创建具有函数签名的函数指针

c++ - 你能从 C++ 调用 CLR 注入(inject)到 SQL Server 中吗?

swift - 在不同场景中声明类变量

Python相互依赖类(循环依赖)

c++ - QThread::run() 中的 std::condition_variable 用法

c++ - 如何组织隐藏父窗口的父子窗口系统。 QT

c++ - 计算矩阵中对角线的总和

c++ - 用数组重载运算符 += C++

c++ - std::vector : 无法将 'std::ostream {aka std::basic_ostream<char>}' 左值绑定(bind)到 'std::basic_ostream<char>&&'

c++ - STL 中的 remove_neighbors 就像时尚