I have a line to pharse like this:

1.2 2 3 4 5
2.4 2.4 2.6 3.5 5.6
3.7 3.4 4.7 3.4 56

I need to sort them in a map of string and vector<double>
The problem I'm having is I'm trying to get each colum and store it in
a vector. But what I'm soring is column.

vector<double> d_vec
map <string, vector<double> > data;
double x;
while (cin >> x)
{
  d_vec.push_back(x); // This will push_back each row not column, how to get column.
}

// I have the string keys here. so I don't have to worry about the keys.
for (iter through the string I have as keys)
{
  data[keys]= d_vec;
}

Now if I do this:

for (map_iter = data.begin(); map_iter != data.end(); map_iter++)
{
   cout << map_iter->first <<endl;

   for (vec_iter = data->second.begin(), vec_iter != data->second.end(); vec_iter++)
  {
     cout << *vec_iter << endl;
  } 

}

Here there out put shoud be:
Value1
1.2
2.4
3.7

Value2
2
2.4
3.4

value3
4
3.5
3.4

value4
5
5.6
56

Recommended Answers

All 6 Replies

I don't understand:
1. ... your sorting criteria. Values in (columns?) #1, #2, #4 are in increasing order but in #3 in decreasing. Why?
2. You have used a map. What for?

Sorry if my quesiton is not understandable.

Here is the data:
DEPTH GR HPI EFI
1900 12 0.13 0.234
1901 12 0.20 0.27
1902 11 0.34 0.24
1903 10 0.22 0.45
1904 11 0.25 0.45
1905 12 0.23 0.43
1906 15 0.22 0.41
1907 16 0.23 0.23


Asume that i have a function to get the keys and store it in an object.

typdef vector<double> Data;
typdef map<string, Data>  MapData;

I need a function to get the colum and store it to the its key.

Data data;
MapData map_data;

Data::const_iter d_iter;
MapData::const_iter m_iter;

double x;
while (cin >> x ) // assumming I'm reading the second line now.
{
   data.push_back(x); // Here I'm storing the row. But I need to store the column.
}

Keys keys; // this is the key string vector of the first line;
Key::const_iterator k_iter

for (k_iter = keys.begin(); k_iter != keys.end(); k_iter++)
{
   map_data[k_iter] = data;
}

What I want to get at the end is:

vector<double> print = map["Depth"];
vector<double>iter;
for (iter = print.begin(); iter != print.end(); iter++)
{
  cout << *print << endl;
  /*
    This should print:
    1900          
    1901          
    1902          
    1903         
    1904         
    1905          
    1906         
    1907           
 
  but I'm getting this, which I don't want:
   1900          
   12        
   0.13     
   0.234
   
*/
}

Here I posted what I meant, I hope it is more understandable now.

Well, you have map<string,vector>. Where is a code to get data in the map? Why string type used for the 1st number of a data line? Why while (cin >> x) ? This loop reads the file until eof occured...

May be this example helps to redesign your code:

typedef map<string,vector<double> > Map;
    Map::const_iterator imap;
    Map m;
    vector<double> v;
    // Test Map initialization
    v.push_back(11);
    v.push_back(12);
    m.insert(Map::value_type("1st",v));

    v.clear();
    v.push_back(21);
    v.push_back(22);
    m.insert(Map::value_type("2nd",v));
    // Print the 2nd column of this strange "matrix":
    for (imap = m.begin(); imap != m.end(); ++imap)
    {
        cout << (*imap).first << "\t" << (*imap).second[1] << endl;
    }

Let me make it more simpler.

I have a file: data.txt
data.txt
1905 12 0.23 0.43
1906 15 0.22 0.41
1907 16 0.23 0.23

now I need to read each row and place it in the correct colum.

typedef vector<double> Data;
typdef map<int, Data>   MapData;

double x;
Data data;
MapData map_data;
while (cin >> x)
{
  data.push_back(x);
}

for (int i = 0; i < 3; i++)
{
  map_data[i] = data;
}

Data::const_iterator iter d_iter;
MapData::const_iter m_iter;

for (m_iter = data.begin(); m_iter = data.end(); m_iter++)
{
  cout << iter->first << endl;
 for (d_iter = data->second.begin(); d_iter=  data->second.end(); d_iter++)
 {
   cout << *d_iter << endl;
 }
}

//This will output
1
1905
12
0.23
0.43

2
1906
15
0.22
0.41

3
1907
16
0.23
0.23

I want to get this out but
1
1905
1906
1907

2
12
15
16

3
0.23
0.22
0.23

4
0.43
0.41
0.23

now I need to read each row and place it in the correct colum.

Correct column of what?.. What is it CORRECT column?

//This will output

No, the snippet can't output anything: you can't compile this pseudocode.
In any case it seems no need in map data structure.

Do you want to print separately all 1st numbers of each file record (row?) then all 2nds and so on?

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.