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
-
-
-
7 1530
You can use a vector of maps to store your data. But you have to organize the data into a structure I guess.
ex: -
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).
-
};
-
-
Now, you have a way of comparing two structs. -
//try a map.
-
typedef map<TwoLetters, int, less<TwoLetters> > MyMap;
-
typedef map<TwoLetters, int, less<TwoLetters> >::iterator MyIterator;
-
-
//create the vector
-
vector<MyMap*>* v;
-
Now you can check if a certain line has the combination {S,Z} by, -
//The wanted element
-
TwoLetters tLetters = {'S', 'Z'};
-
-
//The wanted line
-
int line = wantedLine;
-
-
//Get the relevant map
-
MyMap* wantedMap = (*v)[wantedLine];
-
-
//Find element
-
MyIterator it = wantedmap->find(tLetters);
-
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
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.
Here data.first is the lirst letter and data.second is the second letter.
The map would be:
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.
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?
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.
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?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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....
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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....
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |