Hmm, I don't know why you're fixated on using a map.
Why not create your own object/class?
class Something
{
public:
char letter; //a
int length; //3
string pos; //start
}
And then store this in a vector?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Yep, and thinking out aloud...
I assume you have to add the third parameter pos which will either be single, start-end or start-middle-end and no other case.
Therefore if you sort your file by letter so that
you have:
a 1 start
a 5 end
b 3 start
b 4 middle
b 6 end
c 20 single
Your algo would to count the number of entries per letter.
if count = 1 then
single
else if count = 2 then
start end
else if count = 3 then
start middle end
else
discard (bad data)
And the following snippet might be useful when you come to custom sort your class... http://www.daniweb.com/code/snippet217098.html
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
For example....
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Foo
{
public:
string name;
double length;
};
//This functions sort by name THEN length
bool SortFooByNameThenLength ( const Foo& left, const Foo& right )
{
if ( left.name > right.name )
{
return false;
}
else if ( left.name < right.name )
{
return true;
}
/*if letters are both the same now go to length and sort by
that instead
*/
else
{
/*note the difference, left.LENGTH
instead of left.NAME
*/
if (left.length > right.length )
{
return false;
}
else
{
return true;
}
}
}
int main()
{
Foo test;
vector <Foo> myFoo;
test.name = "a";
test.length = 1;
myFoo.push_back(test);
test.name = "b";
test.length = 5;
myFoo.push_back(test);
test.name = "a";
test.length = 5;
myFoo.push_back(test);
test.name = "b";
test.length = 6;
myFoo.push_back(test);
test.name = "b";
test.length = 3;
myFoo.push_back(test);
test.name = "c";
test.length = 20;
myFoo.push_back(test);
// call the sort like this
std::sort(myFoo.begin(), myFoo.end(), SortFooByNameThenLength);
for ( unsigned int i = 0; i < myFoo.size(); i++ )
{
cout <<myFoo[i].name << " " << myFoo[i].length << endl;
}
cin.get();
return 0;
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439