0

In continuation to this question I am trying to access a map. But i am getting a segmentation fault. Below is my code:

typedef multimap<string, vector<string> > mos_map;
typedef multimap<string, vector<string> >::iterator mos_map_it;

int main()
{

mos_map mos;
mos_map_it it;

vector<string> v1;

v1.push_back("a");
v1.push_back("b");
v1.push_back("c");
v1.push_back("mo1");

mos.insert(mos_map::value_type(*(v1.end()-1),v1));

for(it=mos.begin();it!=mos.end();it++);
{
cout<<(*it).first<<endl;//seg fault occurs here
}
2
  • FWIW, you can write it->first. Commented Jun 28, 2013 at 10:36
  • Even this(it->first) resulted in a core dump Commented Jun 28, 2013 at 10:37

1 Answer 1

4
for(it=mos.begin();it!=mos.end();it++);
//                                    ^

Your loop has empty body.

Some tips:

  • Enable warnings:

    warning: for loop has empty body [-Wempty-body]

  • Declare variables only when they are needed:

    for(auto it = mos.begin(); it != mos.end(); it++);
    {
        cout << (*it).first << endl;
    }
    

    This code will cause a compile time error:

    error: use of undeclared identifier 'it'

Sign up to request clarification or add additional context in comments.

1 Comment

@BalogPal, Yeah, I was adding it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.