How do I add elements to the map of deque of classes?

Hello,

I am kinda stuck here. I'll appreciate some help -- TIA.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<string>
#include<deque>
#include<map>
using namespace std;

class InfoClass
{
public:
     float price;
}

int main()
{
    map<string, float> curr_map;
    map<string, float>::iterator curr_it;
    curr_map["BAC"] = 10.55;

    map<string, deque<InfoClass> > curr_map2;
    map<string, deque<InfoClass> >::iterator curr_it2;
    curr_map2["BAC"] = 10.55;  //-- ??
}
Assuming that the float value has to be added to the back of the deque<>:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct InfoClass
{
    explicit InfoClass( float f ) : price(f) {}
    // ...
    float price;
};

int main()
{
    std::map< std::string, std::deque<InfoClass> > curr_map2 ;

    curr_map2["abc"].emplace_back(10.55) ; // C++11

    curr_map2["bac"].push_back( InfoClass(10.55) ) ; // C++98
}
Last edited on
I should have figured this myself. Thank you!

I changed it a bit to use c'tor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<string>
#include<deque>
#include<map>
using namespace std;

class InfoClass
{
public:
     InfoClass(float p) { price = p; }
     float price;
};

int main()
{
    map<string, deque<InfoClass> > curr_map2;
    map<string, deque<InfoClass> >::iterator curr_it2;
    curr_map2["BAC"].push_back( InfoClass(10.8) );
    curr_map2["ABC"].push_back( InfoClass(10.5) );
    curr_map2["BAC"].push_back( InfoClass(10.3) );
}
I had to re-open this. How do I insert a new element without using key value as an index?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<string>
#include<deque>
#include<map>
using namespace std;

class InfoClass
{
public:
     InfoClass(float p) { price = p; }
     float price;
};

int main()
{
    map<string, deque<InfoClass> > curr_map2;
    map<string, deque<InfoClass> >::iterator curr_it2;
    curr_map2["BAC"].push_back( InfoClass(10.8) );  // works
    curr_map2["ABC"].push_back( InfoClass(10.5) );  // works
    curr_map2["BAC"].push_back( InfoClass(10.3) );  // works

    // All three ways gives an error:
    //  error: no matching function for call to ‘std::deque<InfoClass>::deque(InfoClass)’
    curr_map2.insert(std::pair<string, deque<InfoClass> >("IBM", InfoClass(22.22)));
    curr_map2.insert((map<string, deque<InfoClass> >::value_type("IBM", InfoClass(22.22)));
    curr_map2.insert(std::make_pair("IBM", InfoClass(22.22)));

}
Topic archived. No new replies allowed.