简体   繁体   English

在C ++中映射内部结构

[英]map inside structure in c++

I defined a structure and a vector of the structure.I took input by a temporary structure of the type bind and pushed it back into the vector. 我定义了一个结构和该结构的向量,然后通过类型为bind的临时结构获取输入并将其推回到向量中。

typedef struct {
        string bindername ;
        map<string ,long long int> m ;
        int index ;
    }bind ; 
    vector <bind> v ;

Inside main function , I declared an iterator it as : 在main函数内部,我将其声明为迭代器:

map <string ,long long int>::iterator it ;

When I am trying to access vector's member by iterator it , I am having errors . 当我尝试通过迭代器访问vector的成员时,出现错误。 I tried to access it like this : 我试图这样访问它:

int l = v.size () ;
for (int K = 0 ; K < l ; K++)
        {
            for (it = v[K].m.begin () ;it != v[K].m.end () ; it++) // this line is generating errors
            {
                if ((it->second ()) < b) //this line is also generating errors // b is an int
                {
                    cout << it -> first () << endl ;
                    c++ ;
                }
            } 

Errors generated : 产生的错误:

b.cpp: In function ‘int main()’:
b.cpp:84:22: error: ‘it.std::_Rb_tree_iterator<_Tp>::operator-> [with _Tp = std::pair<const std::basic_string<char>, long long int>, std::_Rb_tree_iterator<_Tp>::pointer = std::pair<const std::basic_string<char>, long long int>*]()->std::pair<const std::basic_string<char>, long long int>::second’ cannot be used as a function
b.cpp:86:27: error: no match for call to ‘(const std::basic_string<char>) ()’

How can I access V[K].m's second elements by iterator it to check whether it is less then b or not ? 如何通过迭代V [K] .m的第二个元素来检查它是否小于b?

You do it->second () which is a function call, but the second member is not a function, it's an integer. 您可以执行it->second () ,这是一个函数调用,但是second成员不是函数,它是一个整数。 You do the same with the first element of the iterator two lines down. 对迭代器的first元素向下两行执行相同的操作。

The two public members of std::pair are objects, not functions. std::pair的两个公共成员是对象,而不是函数。

So it->second() should be it->second . 所以it->second()应该是it->second

if ((it->second ()) < b)
//              ^^

You have a set of parenthesis so the compiler thinks you're incorrectly calling a function. 您有一组括号,因此编译器认为您在错误地调用函数。

it->second < b

第二个是数据成员,而不是函数。

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

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