C++ 2d unordered_map with vector - c++

im coming from PHP where i would do
$um['Im a string'][1] = 3;
for a 2d associative array where the first key is a string, the second an integer and the value is an integer as well. I try to do the same in c++. here is my attempt:
// experiment.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <unordered_map>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::unordered_map;
int _tmain(int argc, _TCHAR* argv[])
{
unordered_map <string,vector<int,int>> um;
um["Im a string"][1] = 3;
printf("Out: %d", um["Im a string"][1]);
return 0;
}
obviously its not the right syntax;

vector<int,int> isn't correct (vector isn't an associative container), you probably want a nested unordered_map<int>. So:
unordered_map <string,unordered_map<int,int>> um;

Related

Problems using std::vector with boost::any in C++

I am trying to create a vector where I will be storing different types of elements (in my case multidimensional arrays) in C++.
I can store the multi-dimensional arrays in the vector but when I try to retrieve through cast::any I receive errors. Let me illustrate all these with a very short example:
#include <iterator>
#include <string>
#include <iostream>
#include <algorithm>
#include <list>
#include <boost/any.hpp>
#include "boost/variant.hpp"
#include "boost/multi_array.hpp"
using namespace std;
using blaze::DynamicMatrix;
using boost::multi_index_container;
using namespace boost::multi_index;
main(){
//Creation of a 3-dimensional array of int
typedef boost::multi_array<int, 3> array_type;
array_type C(boost::extents[3][4][2]);
C[1][1][1] =222;
//Creation of vector and insertion of the 3-d array in it
std::vector<boost::any> vector;
vector.push_back(C);
// Trying to acess a stored element of the array from the vector
cout << endl << "print element C[1][1][1] = " <<boost::any_cast<array_type24> (vector[0][1][1][1]) ;
return 1;
}
So, when I run this I receive the following error:
"no match for ‘operator[]’ (operand types are ‘__gnu_cxx::__alloc_traits >::value_type {aka boost::any}’ and ‘int’)"
Any ideas how to use correctly the boost::any_cast or what I am doing wrong, would be very much appreciated.

C++ vector of array of strings?

I want to have a dynamic structure which I could iterate on, there will be unknown number of entries and known number of strings for each entry. I thought that vector of array of strings could be the way, however I get error while compiling this:
vector< array<string, 5> >
error: invalid use of incomplete type 'struct std::array<std::basic_string<char>, 5u>'
What am I doing wrong? and if this is kind of the way - how would I add/get values to/from this structure?
Did you include all these three headers?
#include <vector>
#include <array>
#include <string>
This compiles just fine:
#include <vector>
#include <array>
#include <string>
int main(int argc, char const *argv[])
{
std::vector<std::array<std::string, 5> > myVec;
return 0;
}

Unable to tokenize a string and pass to a vector <string>

I am experimenting with CGI in C++. I know that there are libraries which handle basic stuff, but in order to know whats going on under the hood i have been trying to parse the stdin using string datatype ---> tokenize using '= and &' then push_back into a vector. at the latter step, i am receiving segmentation fault. given below is the program where i am using cin>> to obtain user input and so on ..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
vector <string> stuff(0);
vector<string>::iterator it;
char* bufferchar;
string buffer;
char str[size];
cout<<"Content-type: text/html\n\n"
<<"<html>"
<<"<head>"
<<"<title>CGI SCRIPT</title>"
<<"</head>"
<<"<body>"
fgets(str,20,stdin); //20 is expect size of content from html form
puts(str);
cout<<"<p>Now to break this guy apart";
int x=0;
bufferchar = strtok(str,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
while(bufferchar!=NULL){
cout<<bufferchar<<'\n'<<'\n';
bufferchar=strtok(NULL,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
}
cout<<"<br>ok does the vector iterate ?";
for (it=stuff.begin();it!=stuff.end();++it){
cout<<*it;
cout<<"<br> ok man, next <br><br>";
}
cout<<"</body>";
cout<<"</html>";
}

Can you have an array of maps in C++?

If so how? Because currently trying to handle input for multiple players using a map of actions.
std::vector<std::map<a,b> >
or if you want to handle the construction/destruction yourself
std::vector<std::map<a,b>* >
where a and b are the keys/values in your map
For example:
#include <vector>
#include <map>
int main(int argc, char* argv[])
{
std::vector<std::map<int,int>> vecOfMaps;
std::vector<std::map<int,int>*> vecOfMaps2;
return 0;
}

Using STL/Boost to initialize a hard-coded set<vector<int> >

Like this question already asked, I'd like to initialize a container using STL where the elements are hard-coded in the cleanest manner possible. In this case, the elements are a doubly nested container:
set<vector<int> > A;
And I'd like (for example) to put the following values in:
A = [[0,0,1],[0,1,0],[1,0,0],[0,0,0]];
C++0x fine, using g++ 4.4.1. STL is preferable as I don't use Boost for any other parts of the code (though I wouldn't mind an example with it!).
This does use g++ 4.4.1, with -std=c++0x
#include <set>
#include <vector>
using namespace std;
int main()
{
set<vector<int>> A = {{0,0,1},{0,1,0},{1,0,0},{0,0,0}};
}
#include <boost/assign/list_of.hpp>
#include <vector>
#include <set>
using namespace std;
using namespace boost::assign;
int main()
{
set<vector<int> > A;
A = list_of
(list_of(0)(0)(1))
(list_of(0)(1)(0))
(list_of(1)(0)(0));
(list_of(0)(0)(0));
return 0;
}