Hi...
My input file is as follows :-
A<-B<-C<-D
A<-C<-E<-F<-P
.......
.....

What i want is to first store the given input as follows :-
{{{A,B},{B,C},{C,D}},{{A,C},{C,E},{E,F},{F,P}}}
and then make a check if the pair is present in particular subset of a set.
For e.g if the user can give index 0 and pair {B,C} we need to search the first index value i.e {{A,B},{B,C},{C,D}} and return true or false based on its presence.
Till now my code is as follows which reads each line of the given file and retrieve the element values by using tokenizer of boost lib. How to proceed further i tried using vector and map for storing in the above format but it is not helping me much.
In case of any doubts on the problem statement kindly let me know that
Kindly help

void main(int argc,char* argv[])
 
                 std::ifstream datafile(argv[1]);
                  if (!datafile) 
                     {
                      std::cerr << "No " << datafile << " file found" << std::endl;
                         exit(1);
                      }
                      int line_no=0;
                      string first_element;
                      string second_element;
                      int count=0;
                      char_delimiters_separator < char >sep(false, "", "<-");
                      for (std::string line; std::getline(datafile, line);)
                          {
                           tokenizer <> line_toks(line, sep);
                           tokenizer <>::iterator iter = line_toks.begin();
                           line_no++;
                           if (line_toks.begin() == line_toks.end()) 
                               continue;
                          
                           for(;iter!=line_toks.end();iter++)
                              {
                               if(count==0)
                                 { 
                                   first_element=*iter;
                                   count++;
                                   continue;
                                 }
                                else
                                   {
                                     second_element=*iter;
                                    } 
                                     first_element=second_element;
                               
                                    /*******
                                      Now i got the values of first element and second  element correctly how to store it and retrieve in the above mentioned structure 
                                   ***********/                     
    
                                 count++;
                                }//End-for --i 
                           
                           }//End-for getline

Recommended Answers

All 7 Replies

With regard to this only let me know what code need to be written to have
format of this sort{{{A,B},{B,C},{C,D}},{{A,C},{C,E},{E,F},{F,P}}}
Kindly help

Looks like you have a set: {A,B} within another set: {{A,B},{B,C},{C,D}} within another set:
{{{A,B},{B,C},{C,D}},{{A,C},{C,E},{E,F},{F,P}}}

So you have a set of chars within a larger set within a larger set. Three levels of sets: So maybe three separate structs:

struct letterPair          // bottom level
{
     char firstLetter;
     char secondLetter;
};

struct middleLevel
{
     vector <letterPair> letterPairs;
};

struct topLevel
{
     vector <middleLevel> outerSet;
};

That's one idea. There are other ways, obviously, but that is one way to organize it. Seems like each line of input would be one instance of middleLevel. Also looks like the first letter in a letterPair is the second letter in the previous letterPair, so your input file could work with this 3 levels of structs layout. Read the file into these structs, then go from there. I haven't worked with the boost libraries much so I'm not sure what "tokenizer" is, so I can't help you there. Anyway, this is just my two cents.

You understood it correctly. Forget the boost thing for the time being. Also let me know i may use these structure one inside another.

If you mean something like this:

struct struct1
{
.
.
.
     struct struct2
     {
     .
     .
     .
          struct struct3
          {
          .
          .
          .
          }
     }
}

I think there may well be a way that is similar to the above layout, where there is a struct within a struct within a struct. However, I have never done it before so I don't know how. You could do this though:

vector < vector <letterPair> > outsideSet;

outsideSet.at (0).at (1).firstLetter = 'B';

outsideSet.at (0) would refer to the index 0 (i.e. first) set of pairs in your example:
{{A,B},{B,C},{C,D}}

outsideSet.at (0).at (1) would refer to the index 1 (i.e. second) set within that larger set:
{B,C}

outsideSet.at (0).at (1).firstLetter refers to 'B' within {A, B}.

The result is that they are in fact nested in the way that you refer to them.

As suggested by you my current implementation look like

struct LetterPair          
{
 string First_element;
 string second_element;
};

void createset()
{
   ..
  ....
  vector<vector<LetterPair> > Outerset;
  string Firstelement;
  string Secondelement;
  ...
  ....
  Outerset.push_back(Firstelement,Secondelement);  
  ....
}

This is giving me compilation error which i m trying to insert the values of the elements what is the correct way to do this. Also let me know how do i find a particular elements pair of given index from this set.

The set should be of the form {{{A,B},{B,C},{C,D}},{{D,E},{E,F}}}

As suggested by you my current implementation look like

struct LetterPair          
{
 string First_element;
 string second_element;
};

void createset()
{
   ..
  ....
  vector<vector<LetterPair> > Outerset;
  string Firstelement;
  string Secondelement;
  ...
  ....
  Outerset.push_back(Firstelement,Secondelement);  
  ....
}

This is giving me compilation error which i m trying to insert the values of the elements what is the correct way to do this. Also let me know how do i find a particular elements pair of given index from this set.

Yeah, that's going to give you a compilation error. I see you changed LetterPair from characters to strings, which is fine, but not needed, at least not in the example you gave where each element is only one character in length.

You got an error because:

1) You can't push_back a pair of strings. You need to push_back a LetterPair.
2) You can't push_back a LetterPair onto Outerset You need to push_back a VECTOR of LetterPair onto Outerset.

Whether you define MiddleSet as a struct or not, you are still using it, even if you are only using it as a temporary variable. Here's something I wrote up. It compiled and ran without errors:

#include <vector>
#include <string>
using namespace std;

struct LetterPair          
{
 string First_element;
 string second_element;
};


vector<vector<LetterPair> > Outerset;


void createset()
{
  vector <LetterPair> MiddleSet;
  LetterPair letterpair;

  string Firstelement;
  string Secondelement;  
  
  letterpair.First_element = "A";
  letterpair.second_element = "B";
  MiddleSet.push_back (letterpair);
  
  letterpair.First_element = "B";
  letterpair.second_element = "C";
  MiddleSet.push_back (letterpair);

  letterpair.First_element = "C";
  letterpair.second_element = "D";
  MiddleSet.push_back (letterpair);
  
  Outerset.push_back (MiddleSet);
  MiddleSet.clear ();
  
  letterpair.First_element = "D";
  letterpair.second_element = "E";
  MiddleSet.push_back (letterpair);

  letterpair.First_element = "E";
  letterpair.second_element = "F";
  MiddleSet.push_back (letterpair);
  
  Outerset.push_back (MiddleSet);
  MiddleSet.clear ();
}


int main ()
{
    createset ();
    return 0;
}
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.