Reputation:
I am trying to solve this problem: Search multiple list for missing entries. I used a multimap for duplication of keys. Here is my code:
#include <iostream>
#include <map>
#include <list>
#include <utility>
using namespace std;
int main(){
list<char>a;
list<int> b;
multimap<char,int>s;
a.push_back('A');
a.push_back('B');
a.push_back('C');
b.push_back(1);
b.push_back(2);
b.push_back(3);
s.insert(std::pair<char,int>('A',1));
s.insert(std::pair<char,int>('A',2));
s.insert(std::pair<char,int>('B',2));
s.insert(std::pair<char,int>('B',3));
s.insert(std::pair<char,int>('C',1));
s.insert(std::pair<char,int>('C',3));
list<char>::iterator it;
list<int>::iterator IT;
multimap<char,int>::iterator i;
for (i=s.begin();i!=s.end();i++){
for (IT=b.begin();IT!=b.end();IT++){
i=s.find(*IT);
if (i==s.end()){
cout<<(*i).first<< " "<<*IT<<endl;
}
}
}
return 0;
}
It compiled but after I run it it stops running abnormally. I think I am not accessing elements correctly. Please help me. EDIT: i have updated my code
#include <iostream>
#include <map>
#include <list>
#include <utility>
using namespace std;
int main(){
list<char>a;
list<int> b;
multimap<char,int>s;
a.push_back('A');
a.push_back('B');
a.push_back('C');
b.push_back(1);
b.push_back(2);
b.push_back(3);
s.insert(std::pair<char,int>('A',1));
s.insert(std::pair<char,int>('A',2));
s.insert(std::pair<char,int>('B',2));
s.insert(std::pair<char,int>('B',3));
s.insert(std::pair<char,int>('C',1));
s.insert(std::pair<char,int>('C',3));
list<char>::iterator it;
list<int>::iterator IT;
multimap<char,int>::iterator i;
for (it=a.begin();it!=a.end();it++){
for (i=s.begin();i!=s.end();i++){
for (IT=b.begin();IT!=b.end();IT++){
if ((*i).first==*it && ((*i).second!=*IT)){
cout<<(*i).first<< " "<<*IT<<endl;
}
}
}
}
return 0;
}
but here is too much combination then i need so what is problem?i think it should work correctly
Upvotes: 1
Views: 2350
Reputation: 2829
Your problem lies here:
if (i==s.end()){
cout<<(*i).first<< " "<<*IT<<endl;
}
if i is s.end() you cannot access it anymore. It means it is invalid. You need to check that it is NOT s.end()
if( i != s.end() )
That is the reason for your crash. However I have so my problems understanding your intend to make a better recommendation for how to solve your problem.
Edit:
A solution to the problem could be this:
#include <map>
#include <set>
#include <iostream>
struct d {
char c;
int i;
};
int main()
{
d data[] = {
{ 'A', 1 },
{ 'A', 2 },
{ 'B', 2 },
{ 'B', 3 },
{ 'C', 1 },
{ 'C', 3 }
};
std::map< char, std::set<int> > data_map;
for( size_t i = 0; i < sizeof(data)/sizeof(d); ++i ) {
data_map[ data[i].c ].insert( data[i].i );
}
for( char c = 'A'; c < 'D'; ++c ) {
for( int i = 1; i < 4; ++i ) {
if( data_map[c].count( i ) == 0 ) {
std::cout << c << " " << i << " is missing" << std::endl;
}
}
}
return 0;
}
Output:
A 3 is missing B 1 is missing C 2 is missing
Upvotes: 3
Reputation: 2009
Just some hints to start with -
Run the code in a debugger (VS 2008 express is free) - step the code, examine the variables, and you should see whats wrong.
Rename your iterators so it's more clear what you are trying to do - i, it, and IT just lead to confusion.
Finally - write out what you are trying to do before coding it - like this
For (all letters)
{
For (all numbers)
{
if (letter,number not in map)
print out missing (letter, number)
}
}
Upvotes: 1