【问题标题】:STL Map - Displaying what is pointed to by find() functionSTL Map - 显示 find() 函数指向的内容
【发布时间】:2015-07-31 14:48:04
【问题描述】:

出于测试目的,我通过for 循环运行以下代码。实际上只有前三个键存在,并且 “Record found” 与从 findVertex->first 检索的键一起按预期显示。

  • 我的问题是,我如何才能访问指向的第二个值?

findVertex->second似乎很明显,但不起作用,因为第二个值是我创建的对象,如果它有任何用处,它的声明在代码下方给出。

for(int i = 0; i<10; i++)
    {
     map<int, vector<Vertex> >::const_iterator findVertex = vertexMap.find(i);

     if(findVertex != vertexMap.end())
      {
          cout<<"\nRecord found: ";
          cout<<findVertex->first;
          cout<<findVertex->second; //does not work
      }
     else
         cout<<"\nRecord not found";
}

类代码:

class Vertex
{
    private:
        int currentIndex;
        double xPoint, yPoint, zPoint;
        vector<double> attributes;

    public:
        friend istream& operator>>(istream&, Vertex &);
        friend ostream& operator<<(ostream&, Vertex &);
};

谢谢

【问题讨论】:

  • 提供运营商:std::ostream&amp; operator &lt;&lt; (std::ostream&amp;, const std::vector&lt;Vertex&gt;&amp;)

标签: c++ dictionary stl iterator iostream


【解决方案1】:

您的map 属于这种类型

map<int, vector<Vertex>>

这意味着firstintsecondvector&lt;Vertex&gt;

虽然您为Vertex 定义了operator&lt;&lt;,但vector&lt;Vertex&gt; 没有这样的函数。你可以遍历你的vector,如果你可以访问 C++11,你可以使用类似的东西

 if(findVertex != vertexMap.end())
 {
     cout << "\nRecord found: ";
     cout << findVertex->first << '\n';
     for (auto const& vertex : findVertex->second)
     {
         cout << vertex << '\n';
     }
 }

如果您无法访问 C++11,您可以手动执行相同的想法

 if(findVertex != vertexMap.end())
 {
     cout << "\nRecord found: ";
     cout << findVertex->first << '\n';
     for (vector<Vertex>::const_iterator itVertex = findVertex->second.cbegin();
          itVertex != findVertex->second.cend();
          ++itVertex)
     {
         Vertex const& vertex = *itVertex;
         cout << vertex << '\n';
     }
 }

【讨论】:

  • 谢谢。我无权访问 C++11 并使用了手动版本。但是,我遇到了这个错误:error: no match for 'operator=' (operand types are 'std::vector&lt;Vertex&gt;::iterator {aka __gnu_cxx::__normal_iterator&lt;Vertex*, std::vector&lt;Vertex&gt; &gt;}' and 'std::vector&lt;Vertex&gt;::const_iterator {aka __gnu_cxx::__normal_iterator&lt;const Vertex*, std::vector&lt;Vertex&gt; &gt;}')|
  • @CollinOladimeji 立即尝试编辑后的代码。我改为::const_iterator 并使用cbegin()cend()
  • @Collin Oladimeji 查看我的答案。:)
  • @Cyber​​ 显然,error: 'const class std::vector&lt;Vertex&gt;' has no member named 'cbegin'|。我会错过包含吗?
  • @VladfromMoscow 我会看看 :)
【解决方案2】:

首先你不能用const_iterator

map<int, vector<Vertex> >::const_iterator findVertex = vertexMap.find(i);

显示 Vertex,因为您将 operator &lt;&lt; 与第二个参数声明为非常量引用

friend ostream& operator<<(ostream&, Vertex &); 
                                     ^^^^^^^^

你应该像这样声明它

friend ostream& operator<<(ostream&, const Vertex &);
                                     ^^^^^

否则把上面的语句改成下面的

map<int, vector<Vertex> >::iterator findVertex = vertexMap.find(i);
                           ^^^^^^^^ 

并更改此语句

cout<<findVertex->second; //does not work

到下面的代码sn-p

for ( Vertex &v : findVertex->second ) cout << v << endl;

如果你要为第二个参数修改指定限定符 const 的操作符,那么你可以写

map<int, vector<Vertex> >::const_iterator findVertex = vertexMap.find(i);
                           ^^^^^^^^^^^^^^
//...

for ( const Vertex &v : findVertex->second ) cout << v << endl;
      ^^^^^

或者,您可以使用普通循环代替基于范围的 for 语句,例如

for ( std::vector<Vertex>::size_type i = 0; i < findVertex->second.size(); i++ )
{
    std::cout << findVertex->second[i] << std::endl;
}

for ( std::vector<Vertex>::iterator it = findVertex->second.begin(); 
      it != findVertex->second.end(); 
      ++it )
{
    std::cout << *it << std::endl;
}

【讨论】:

  • 为我工作。谢谢!真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 2011-07-08
  • 2018-06-14
  • 2021-07-24
相关资源
最近更新 更多