Hi,

I am used to array of hashes in perl. So I was trying to do something like that in c++. Suppose I have created an array and now I want to store multimap in it. How can I do that ? I need to get some clues about this.

I know that we can put it inside the mutlimap after converting into string but I want to save the index information inside the array also.

This is how I do it in perl normally
Suppose I have an array: {1,2,2,2,4,4,7}
Now I will store it inside a multimap. Then suppose I have to get the position of 2
Just a hypothetical example:

                   array{multimap} == 1 which will be 2
                   array{multimap} == 0 which will be 1

I should be able to able to count the 2's or 3's inside mutlimap with reference to Array and then assign some flags.

Like        array[0] which is present only once will be "only one"
               array[1] which should be "start"
               array[2] should be "middle"
              array[3] should be "end"

In other words if I have a key in my multimap and I want to flag it with reference to my original array howto do that ?

I hope I will get some advice howto do such a thing in c++.
Thanks

Here is my attempt on this:-

My input file looks like this:-

1    100    a    Andrew    USA    5580    mytext
101    200    b    Andrew    USA    780    mytext
201    300    c    John    USA    154    mytext
301    400    d    Collin    USA    880    mytext
401    500    e    John    USA    551    mytext
501    600    f    John    USA    15    mytext

I am trying to get my output something like this:-

1    100    a    Andrew    5580    start
101    200    b    Andrew    780    end
201    300    c    John    154    start
301    400    d    Micheal    880    single_word
401    500    e    John    551    middle
501    600    f    John    15    end

So I am first putting the file into a multimap and then based on the keyword count trying to do the flagging. Obviously I am not able to do it. I would appreciate to help on howto proceed in my code further since I feel a bit stuck.

Here is my code:-

#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;

int main(int argc, char* argv[])
{
  multimap<string, string> mmap;
  multimap<string, string> map2;
  multimap<string, string>::const_iterator m1_cIter;

  string str[7];
  string strkey1;
  string combval;
  string mapping[3];
  ifstream myfile(argv[1]);
  while(! myfile.eof()) {
        for(int i=0; i < 7; i++)
            if(i<6)getline(myfile,str[i],'\t');
               else getline(myfile,str[i]);
                strkey1.assign(str[3]);
                combval.assign(str[0]+"\t"+str[1]+"\t"+str[2]);
                mmap.insert(pair<string, string>(strkey1, combval));
                mapping[2] = strkey1;
  }
       for (multimap<string, string>::iterator it = mmap.begin();it != mmap.end();++it)
       {
                if(mmap.count((*it).first) == 2){
                         //for(int i=0; i<2; i++){
                               map2.insert(pair<string, string>((*it).first, (*it).second));
                                  m1_cIter = map2.begin( );
                                  cout << m1_cIter -> first <<"\t"<< m1_cIter -> second <<"\t" <<"Start"<< endl;


                         //}
                }              
        }
 
                                         
  return 0;
}

When I do something like below I am able to catch the "single".

for (multimap<string, string>::iterator it = mmap.begin();it != mmap.end();++it)
       {
            //cout << (*it).first << "\t" << (*it).second << endl;
              //mapping[0] = (*it).first;
              //mapping[1] = (*it).second;

              //cout << mapping[0] << "\t"<< mapping[1]<< "\t"<< "Start"<<endl;
                if(mmap.count((*it).first) == 1){
                //cout << mmap.count((*it).first) << endl;
                  cout << (*it).first << "\t"<< (*it).second <<"\t"<< "Single"<<endl;
                }

               
        }

I am able to get the "Start" also but the program prints only the first one in a file and exits:-

for(multimap<string, string>::iterator it = mmap.begin();it != mmap.end();++it)
            for(int i=0; i<2; i++)  
                if(mmap.count((*it).first) == 2)

                               map2.insert(pair<string, string>((*it).first, (*it).second));
                                  m1_cIter = map2.begin( );
                                  m1_cIter++;
                                  cout << m1_cIter -> first <<"\t"<< m1_cIter -> second <<"\t" <<"Start"<< endl;

Like here it prints only

1    100    a    Andrew    5580    start

Would appreciate any help.

Thanks

I also get the "END" element by doing these changes:-

for(multimap<string, string>::iterator it = mmap.begin();it != mmap.end();++it)
            //for(int i=0; i<4; i++)
                if(mmap.count((*it).first) > 2)

                               map2.insert(pair<string, string>((*it).first, (*it).second));
                                  m1_cIter = map2.end( );
                                  m1_cIter--;  // to get the last element.
                                  cout << m1_cIter -> first <<"\t"<< m1_cIter -> second <<"\t" <<"Start"<< endl;
                //i++;

Now the only problem again is that I am able to print only once. How can I make it print for the whole file in a loop ?

Thanks in advance

No replies yet ? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.