Map insert is ambiguous - c++

I am trying to use boost::container::map. During inserting data, the error "insert is ambiguous" is shown.
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert("Ram",0);
}

Your style of inserting is not correct. I provide the code:
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert(std::pair<const std::string, int>("Ram",1));
std::cout<< map["Ram"];
return 0;
}

Related

Implementing simple priority queue using vector in C++

The following gives an error for the code mentioned below.
Where I have gone wrong ?
error: ‘function’ is not a member of ‘std’
I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}
#include <functional>
You need this include to get access to std::function
See: http://en.cppreference.com/w/cpp/utility/functional/function

Why cant I insert values into 2D vector when I try to do it in a function in C++?

I am creating a small C++ program for homework. I im trying to populate a 2D vector but when I write matriz[iA][iB]=iNum; it gives me the error "no match for 'operator='"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
using namespace std;
void popularMatriz(int iTamano, vector<vector<int>> *matriz){
for(int iA=0; iA<iTamano; iA++){
for(int iB=0; iB>iTamano; iB++){
int iNum;
scanf("%d", &iNum );
matriz[iA][iB]=iNum;
}
}
}
int main(){
int iTamano;
scanf("%d", &iTamano);
vector<vector<int>> matriz(iTamano, vector<int>(iTamano));
matriz[2][2]=5;
popularMatriz(iTamano, &matriz);
return 0;
}
You're passing a pointer to matriz; so is wrong use it as
matriz[iA][iB]=iNum;
I suggest you to pass it as reference; I mean, define popularMatriz() as
void popularMatriz(int iTamano, vector<vector<int>> & matriz)
and call it without &
popularMatriz(iTamano, matriz);
You're taking a pointer to matriz. Do this instead:
(*matriz)[iA][iB]=iNum;

no member named 'set' in 'std::vector<int>'

I use QT creator and Xcode 4.63 (osx mav)
I just call .set(....) and got following error
no member named 'set' in 'std::vector<int, std::allocator<int> >
seem like something wrong with lib , since I'm new to QT creator i still got no idea how to make it right. (I'm new to c++)
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
vector<int> myVector(10,0);
fstream mystream;
string getStream;
while(getline(mystream,getStream)) {
getline(mystream,getStream);
if(stringToInteger(getStream)>=0 && stringToInteger(getStream)<=9) {
myVector.set(0,(myVector[0]+1))
};
}
return 0;
};
The "set" method doesn't exist for vectors in STL. You could use the [] operator if you want to change a value:
myVector[0] = myVector[0] + 1;
or even
myVector[0]++;

I can't declare a map

So in my cpp file I'm trying to declare a map as follows:
map<string, vector<myStruct>> myMap;
At the top of my file I have written using namespace std and I also have #include <string>
.
However I'm getting these weird errors:
error: ISO C++ forbids declaration of ‘map’ with no type
I don't know how to fix it. If I write #include <map> that just causes the compiler to freak out.
do you have #include <map>? rest looks valid,
however you might need to add a space if your C++ standard is not C++11:
#include <map>
#include <vector>
#include <string>
using namespace std;
map<string, vector<myStruct> > myMap;
^^^
even better not use namespace std:
#include <map>
#include <vector>
#include <string>
std::map<std::string, std::vector<myStruct> > myMap;
You need to include map header file.
#include <map>
Meanwhile, in case you are not using C++11, you need a space:
map<string, vector<myStruct> > myMap;
//^^
You should also include <map>. std::map is introduced through this header.
Furthermore, using namespace std is considered a bad practice. You should either have a using statement or use the prefix the name with std:: to denote a fully-qualified identifier:
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::vector<myStruct>> myMap;
Note, the lack of a using statement ;)
#include <vector>
#include <string>
#include <map>
#include <iostream>
typedef int myStruct;
std::map<std::string, std::vector<myStruct>> myMap;
int
main()
{
std::vector<myStruct> testMe = { 1, 2, 3};
myMap["myTest"] = testMe;
std::cout << myMap.size() << std::endl;
return(0);
}

map in header file c++

Simple question....
I have a header file called bag...
#include <map>
#include <iostream>
#include <string>
using namespace std;
class Bag
{
public:
Bag();
map <const string str, int number> items;
private:
};
#endif
In the implementation, I'd like to insert something into bag:
#include <string>
#include <fstream>
#include <map>
#include "Bag.h"
using namespace std;
Bag::Bag()
{
items["brush"] = 4;
}
But for some reason, i can't access items. What am I doing wrong????
Thanks!
Duh! For some reason I'm trying to insert a value name with the value type field. Thanks Chad, I think you fixed me!
map <const string, int> items;