简体   繁体   English

访问作为映射中的值的向量以使用另一个向量对其运行相等运算符

[英]Accessing a vector which is a value in a map to run the equality operator on it with another vector

I have a vector that is made up of maps which have the following type definition:我有一个由具有以下类型定义的地图组成的向量:

std::map< int, std::vector<int> >

Thus, the definition of the vector itself is:因此,向量本身的定义是:

std::vector< std::map< int, std::vector<int> > >

Now I am creating a vector of integers and inserting it in a map with an integer key and adding the map itself to the outer vector:现在我正在创建一个整数向量,并将它插入一个带有整数键的映射中,并将映射本身添加到外部向量中:

    std::map<int, std::vector<int> > vDescriptorAtom;
    vDescriptorAtom.insert( std::make_pair(498, vListOfIds) );
    messageDescriptorVector.push_back( vDescriptorAtom );

Where vListOfIds itself is a vector of integers.其中 vListOfIds 本身是一个整数向量。

At a later stage I want to extract the inner vector and compare with another vector that I possess.在稍后阶段,我想提取内部向量并与我拥有的另一个向量进行比较。 I was under the impression that I can easily use the == operator between the two vectors to do the comparison.我的印象是我可以轻松地在两个向量之间使用==运算符进行比较。 But I am having some trouble doing this.但是我在这样做时遇到了一些麻烦。

I tried:我试过:

               for ( int i = 0 ; i <= messageDescriptorVector.size(); i++ )
                {    
                    if ( current_tag_stack == (messageDescriptorVector.at(i))->second )
                    {
                        vFoundMatchingDescriptor = true;
                        break;
                    }
                } 

Note that current_tag_stack has the following definition:请注意, current_tag_stack 具有以下定义:

std::vector<int> current_tag_stack;

But I am getting this error:但我收到此错误:

base operand of â->â has non-pointer type âstd::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > >â

What am I doing wrong ?我做错了什么?

EDIT编辑

As suggested by a comment, I tried to access the vector that I have stored inside the map as: messageDescriptorVector.at(i).second but it is giving me this error: âclass std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > >â has no member named âsecondâ正如评论所建议的那样,我尝试访问存储在地图中的向量: messageDescriptorVector.at(i).second但它给了我这个错误: âclass std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > >â has no member named âsecondâ

messageDescriptorVector.at(i) is a map. messageDescriptorVector.at(i)是一张地图。 You have to obtain an element of that map, in order to get it's second and compare it with your reference vector.您必须获得该地图的一个元素,以便获得second元素并将其与您的参考向量进行比较。 So you need one more level of iteration.所以你需要多一层迭代。

for ( int i = 0 ; i < messageDescriptorVector.size(); i++ )
{
  auto& m = messageDescriptorVector.at(i); // m is a map<int, vector<int>>

  for (auto& p : m) // p is a ref to pair<int, vector<int>>
  {
    if ( current_tag_stack == p.second )
    {
      // do something ....

or just或者只是

for (auto& m : messageDescriptorVector)
{
  for (auto& p : m)
  {
    if ( current_tag_stack == p.second )
    {
      // do something ....

Aside of issue that you are trying to accessing member second on a map, I would recommend to use typedefs in your code.除了您尝试在地图上访问第二个成员的问题之外,我建议在您的代码中使用 typedef。 It will not only simplify your code, but will help to change data structure if necessary.它不仅会简化您的代码,而且会在必要时帮助更改数据结构。 In your case all of this definitions all around your code:在您的情况下,所有这些定义都围绕您的代码:

std::map< int, std::vector<int> >
std::vector< std::map< int, std::vector<int> > >
std::map<int, std::vector<int> > vDescriptorAtom;
std::vector<int> current_tag_stack;

Compare it with:与它进行比较:

typedef std::vector<int> tag_stack;
typedef std::map<int,tag_stack> tag_stack_map;

tag_stack_map vDescriptorAtom;
tag_stack current_tag_stack;

And imagine one day you would need to change your tag_stack from std::vector to something else.并想象有一天您需要将 tag_stack 从 std::vector 更改为其他内容。 Where it will be easier?哪里会更容易?

function at() returns a reference to the element at specified location pos, so you should write:函数 at() 返回对指定位置 pos 元素的引用,因此您应该编写:

(messageDescriptorVector.at(i)).second

I preferred write this code like:我更喜欢写这样的代码:

std::map<int, std::vector<int>> vDescriptorAtom;
std::vector<std::map<int, std::vector<int>>> messageDescriptorVector;

vDescriptorAtom[498] = vListOfIds;
messageDescriptorVector.push_back( vDescriptorAtom );

// do something

for(auto& maps : messageDescriptorVector)
    for(auto& pairs : maps)
        if (current_tag_stack == pairs.second)
            // do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM