473,500 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in choosing data structure

4 New Member
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




Expand|Select|Wrap|Line Numbers
  1.  
  2.                 void main(int argc,char* argv[])
  3.  
  4.                  std::ifstream datafile(argv[1]);
  5.                   if (!datafile) 
  6.                      {
  7.                       std::cerr << "No " << datafile << " file found" << std::endl;
  8.                          exit(1);
  9.                       }
  10.                       int line_no=0;
  11.                       string first_element;
  12.                       string second_element;
  13.                       int count=0;
  14.                       char_delimiters_separator < char >sep(false, "", "<-");
  15.                       for (std::string line; std::getline(datafile, line);)
  16.                           {
  17.                            tokenizer <> line_toks(line, sep);
  18.                            tokenizer <>::iterator iter = line_toks.begin();
  19.                            line_no++;
  20.                            if (line_toks.begin() == line_toks.end()) 
  21.                                continue;
  22.  
  23.                            for(;iter!=line_toks.end();iter++)
  24.                               {
  25.                                if(count==0)
  26.                                  { 
  27.                                    first_element=*iter;
  28.                                    count++;
  29.                                    continue;
  30.                                  }
  31.                                 else
  32.                                    {
  33.                                      second_element=*iter;
  34.                                     } 
  35.                                      first_element=second_element;
  36.  
  37.                                     /*******
  38.                                       Now i got the values of first element and second  element correctly how to store it and retrieve in the above mentioned structure 
  39.                                    ***********/                     
  40.  
  41.                                  count++;
  42.                                 }//End-for --i 
  43.  
  44.                            }//End-for getline 
  45.  
  46.  
  47.  
Jan 23 '08 #1
7 1530
DumRat
93 New Member
You can use a vector of maps to store your data. But you have to organize the data into a structure I guess.

ex:

Expand|Select|Wrap|Line Numbers
  1. struct TwoLetters
  2. {
  3.    char first;
  4.    char second;
  5.  
  6.    //Overload the operator < or > here. There's another way with a function, but I haven't tried it. Let's say that you overloaded the < (less operator).
  7. };
  8.  
  9.  
Now, you have a way of comparing two structs.


Expand|Select|Wrap|Line Numbers
  1. //try a map.
  2. typedef map<TwoLetters, int, less<TwoLetters> > MyMap;
  3. typedef map<TwoLetters, int, less<TwoLetters> >::iterator MyIterator;
  4.  
  5. //create the vector
  6. vector<MyMap*>* v;
  7.  
Now you can check if a certain line has the combination {S,Z} by,

Expand|Select|Wrap|Line Numbers
  1. //The wanted element
  2. TwoLetters tLetters = {'S', 'Z'};
  3.  
  4. //The wanted line
  5. int line = wantedLine;
  6.  
  7. //Get the relevant map
  8. MyMap* wantedMap = (*v)[wantedLine];
  9.  
  10. //Find element
  11. MyIterator it = wantedmap->find(tLetters);
  12.  
if the element is in the map, it points to the element (pair). If not, it points to the end of the map. You can check it and find this out. Hope it helped.

DumRat
Jan 23 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can use a vector of maps to store your data. But you have to organize the data into a structure I guess.

ex:


Code: ( text )
struct TwoLetters
{
char first;
char second;

//Overload the operator < or > here. There's another way with a function, but I haven't tried it. Let's say that you overloaded the < (less operator).
};
The map requires a pair<> conrtainer for the entries.

Expand|Select|Wrap|Line Numbers
  1. pair<char, char> data;
  2.  
Here data.first is the lirst letter and data.second is the second letter.

The map would be:
Expand|Select|Wrap|Line Numbers
  1. map<char, char> theMap;
  2.  
In the map data.first is the key and data.second is a value associated woith that key.

If this does not fit your data model, then don't use a map.
Jan 23 '08 #3
DumRat
93 New Member
WeaknessForCats << On the above problem I guess your method is the best. But say the set of character pairs are like this :

{A,B}, {A,E}, {A,F}, {C,K}, {C, G}, ....

Now can a map be used in the way you have indicated?
Jan 24 '08 #4
Laharl
849 Recognized Expert Contributor
You could use a single map there, a map<char, char>, assuming your desire is to get the second character if the first is inputted.
Jan 24 '08 #5
DumRat
93 New Member
You could use a single map there, a map<char, char>, assuming your desire is to get the second character if the first is inputted.
Suppose you want to check if there's a certain character pair {X,Y} in the map?
Jan 24 '08 #6
Laharl
849 Recognized Expert Contributor
At that point...since a map's [] give you the value, you can check if the second value appears when you put in the first. Or you could use a vector<pair<char> >, I guess.
Jan 24 '08 #7
DumRat
93 New Member
At that point...since a map's [] give you the value, you can check if the second value appears when you put in the first. Or you could use a vector<pair<char> >, I guess.
But this is the point. Say u have

{A,B}, {A,C}, etc.

If u use the first character as the key, you can only have one of these in the map. So u can't use a map like that. The other alternative is to use a multimap - but then, if you had 25 pairs starting with A, you'd have to traverse through that entire 25 elements to find your element - which is inefficient. The best method then is to use both the characters combined as the key. That's what I did in my first post.
Jan 25 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2376
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
0
3908
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
7994
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
2
4426
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
19
1558
by: ballpointpenthief | last post by:
At the moment, I only write functions which accept pointers as arguments in order to change a value, but I've realised it would often be a lot more effiecient to pass a pointer rather than an...
7
3107
by: rAinDeEr | last post by:
I have a Parts Table with the following structure.. I need to decide which makes the primary key of the table. I cant have any sequences nor can I add any new fields in this Table. Table...
6
3510
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
3793
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
43
2310
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.