Extracting data from a map to a vector


MPD78
05-04-2010, 02:09 PM
Hello all,

I am having trouble extracting data from a map to a vector.

Here is my code

// Needed #includes

#include <map>

int main()
{
int pause; // dummy variable to keep the output window open

map<double,double> SA_106_A; // Map containg the temperature and stress value

// Temperaure and corrsponding stress values
SA_106_A.insert(make_pair(650,13.7));
SA_106_A.insert(make_pair(700,12.5));
SA_106_A.insert(make_pair(750,10.7));

// Vector to store the extracted data in
vector<double> stress(2);

typedef map<double,double>::iterator stress_it;

for(stress_it p = SA_106_A.begin(); p!=SA_106_A.end(); ++p){
double stress_temp = p->second;
for(int i =0; i<stress.size(); i++){
stress[i] = stress_temp;
}


}

// Send the stored data out to the screen
for(int i=0; i<stress.size(); i++){
cout << stress[i] << endl;
}

// Keep the output window open
cin >> pause;

return 0;
}

The output I obtain is:

10.7
10.7

This is the last stress value twice.

I am not sure what the best way is to do this, any guidance would be welcome.

Thanks
Matt

davekw7x
05-04-2010, 04:21 PM
...way ... to do this...

#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main()
{

map <double, double> SA_106_A; // Map containg the temperature and stress value

// Temperaure and corrsponding stress values
SA_106_A.insert(make_pair(650, 13.7));
SA_106_A.insert(make_pair(700, 12.5));
SA_106_A.insert(make_pair(750, 10.7));

// Vector to store the extracted data in
vector <double> stress; // An empty vector
cout << "stress.size() = " << stress.size() << endl;

typedef map < double, double >::iterator stress_it;

cout << "Before the assignment loop, stress.size() = " << stress.size() << endl;

for (stress_it p = SA_106_A.begin(); p != SA_106_A.end(); ++p) {
cout << " pushing " << p->second << " onto the stress vector" << endl;
stress.push_back(p->second);
}
cout << endl;

cout << "After the assignment loop, stress.size() = " << stress.size() << endl;

for (unsigned i = 0; i < stress.size(); i++) {
cout << " stress[" << i << "] = " << stress[i] << endl;
}

cout << "Press 'Enter' to continue . . . ";
while (cin.get() != '\n')
;

return 0;
}


Output:

Before the assignment loop, stress.size() = 0
pushing 13.7 onto the stress vector
pushing 12.5 onto the stress vector
pushing 10.7 onto the stress vector

After the assignment loop, stress.size() = 3
stress[0] = 13.7
stress[1] = 12.5
stress[2] = 10.7
Press 'Enter' to continue . . .



Regards,

Dave

Footnote: Minus one point for ending a comment with a preposition.

MPD78
05-04-2010, 05:33 PM
Thanks again Dave.

Matt

MPD78
05-05-2010, 08:50 AM
Dave,

I thought it would be easy to read the same information into a VecDoub variable but I am having some trouble. Could you show me how to read the information into a VecDoub?

Thanks
Matt

davekw7x
05-05-2010, 12:54 PM
Dave,

I thought it would be easy to read the same information into a VecDoub variable but I am having some trouble. Could you show me how to read the information into a VecDoub?

Thanks
Matt
The VecDoub class, unlike std::vector, does not have push_back() defined, and it is not automatically and easily resizable.

One way: Determine what the size must be then create a VecDoub object of that size. Then use assignment with the index [] notation. (You could have done this with the std::vector object, but I like push_back() for a couple of reasons.)


#include "../code/nr3.h"
#include <map>

int main()
{

// Map containing the temperature and stress value
map <double, double> SA_106_A;

// Temperaure and corrsponding stress values
SA_106_A.insert(make_pair(650, 13.7));
SA_106_A.insert(make_pair(700, 12.5));
SA_106_A.insert(make_pair(750, 10.7));

// Vector where extracted data is stored
VecDoub stress(SA_106_A.size());

cout << "stress.size() = " << stress.size() << endl;

typedef map < double, double >::iterator stress_it;

Int i = 0;
for (stress_it p = SA_106_A.begin(); p != SA_106_A.end(); ++p) {
cout << " Assigning " << p->second << " to stress["
<< i << "]" << endl;
stress[i++] = (p->second);
}
cout << endl;

cout << "After assignment loop:" << endl;
for (i = 0; i < stress.size(); i++) {
cout << " stress[" << i << "] = " << stress[i] << endl;
}

cout << "Press 'Enter' to continue . . . ";
while (cin.get() != '\n')
;

return 0;
}


Output

stress.size() = 3
Assigning 13.7 to stress[0]
Assigning 12.5 to stress[1]
Assigning 10.7 to stress[2]

After assignment loop:
stress[0] = 13.7
stress[1] = 12.5
stress[2] = 10.7
Press 'Enter' to continue . . .


Regards,

Dave

MPD78
05-06-2010, 08:15 AM
Thanks Dave.

All works fine.

Matt