I assigned array
char words[100][100];
Now I want to save a word and its pos in the line.
Say line has "hi Iam a programmer". Now I want to save
string word;
while(line){
//called a function to get the word and position.
words[word]["pos"] = pos;
}
I have split the words and saved in the string word, but when I try to save I get error.
"No viable overloaded operator[] for type 'char[100][100]"
What am I doing wrong?
You are trying to use array as a map. You can't use strings as array index. The structure you need is std::map<std::string, std::map<std::string, int> >
std::map<std::string, std::map<std::string, int> > m;
m["foo"]["bar"] = 10;
char[100][100] is a multidimensional array of single characters, which can be used to store fixed-length strings. It can be indexed using integer variables, not strings.
It looks like you want to use std::map<std::string, std::map<std::string, int> > or similar.
You cannot use a string as an index in C++ arrays. What you need is a map:
std::map< std::string, std::map<string, int> > words;
Then you have to:
words[word]["pos"] = pos;
But, what other data would you be saving other than pos? If not, then why do you want to make it a 2 dimensional data structure? Can't you just:
positions[word] = pos;
Where positions is of type std::map<std::string, int>.
EDIT: As pointed out by Mike, not using pointers anymore.
Related
I am trying to construct a map that maps a string to a vector of unsigned integers. The way that I construct this map is as follows:
void PixelP1ROCDACSettings::getDACs(map<string,vector<unsigned int>>& dacs) const
{
dacs.clear();
dacs.insert(pair<string, vector<unsigned int> > (k_DACName_Vdd, make_vector(Vdd_, k_DACAddress_Vdd)));
dacs.insert(pair<string, vector<unsigned int> > (k_DACName_Vana,make_vector(Vana_, k_DACAddress_Vana)));
...
}
Where make_vector is defined as follows:
std::vector<unsigned int> make_vector(unsigned int DACValue,
unsigned int DACAddress) const ;
My questions are:
1) I would like to get access each individual value in my vector, I've tried to do,
dacs[key][index] but that didn't seem to work. Is there a special syntax to do this?
2) Additionally, I would like to iterate across my map, How would I do that?
Thanks in advance.
If you are using c++11 you can iterate with
for (auto& keyvaluepair : dacs) {
//keyvaluepair.first is your string
//keyvaluepair.second is your vector
}
also dacs[key][index] is the correct way to access the indexth element in the vector mapped to by key.
In c++, I would like to insert a vector in a map.
The key of the map is a pair of string and int, and the value of one is a vector.
I am writing down the following code, however it seems that the vector is not inserted into the map.
Is the syntax of the code is wrong?
If so, could you tell me correct one?
map<pair<string, int>, vector<string> > my_map;
vector<string> v;
v.push_back("abcde");
my_map.insert(make_pair(make_pair("aaa",1),v));
You have used the v_pre while vector is of name v:
my_map.insert(make_pair(make_pair("aaa",1),v_pre));
The correct code should be:
my_map.insert(make_pair(make_pair("aaa",1),v));
i want my list to hold an integer value as well as a string value. is this possible?
I am implementing a hash table using STL lists which can store only the integer. I am hashing a string to get the index where i am storing my integer. Now i want my string to be stored with the integer as well.
EDIT 1:
so i am using this statement:
list<pair<int,string>> table[127];
and here is the error im getting:
>>' should be> >' within a nested template argument list
ok i fixed this.. it seems i didn't put a space in the ">>" so now its fix
next question
how do i add my pair to the table array?
You can have a list of std::pairs or, with c++11, std::tuple, for example:
std::list < std::pair< int, std::string > >list;
std::list < std::tuple< int, std::string > >list;
To access the elements inside a pair, use pair.first and pair.second. To access the elements inside a tuple, use std::get:
auto t = std::make_tuple(1,"something");
std::get<0>(t);//will get the first element of the tuple
You can use std::pair or std::tuple,
std::list<std::pair<int, string>> list;
You can store the string and the integer in a structure and store the objects of the structure.
Each list element can look like:
struct element {
string str;
int val;
};
This is the C way to handle, please #SingerOfTheFall's answer also.
I am trying to access a specific element out of a std::map with more than two elements. Here is an example:
std::map <int, CString, CString, CString> map;
//Initialise
map[0] = _T("stuff1"), _T("stuff2"), _T("stuff3");
//now if I just want to access stuff3 is it this:
CString str = map[0][2];
//or something more like this?
CString str = map[0]. ???
Any help would be great thanks.
edit: Thanks sorry about that, first time using maps, I was wondering why I couldn't find any information on std::map 's with more elements inside.
Have you tried to compile this? It shouldn't.
You can create only a map with exactly 1 key and 1 value for each element.
But the value can be compound, so you can write
struct ValueType {
CString v1;
CString v2;
CString v3;
}
std::map <int, ValueType> map;
and access elements like map[somekey].v3;
To insert a value in such a map, you'll have to write
ValueType strings = {"1","2","3"};
map.insert(999, strings);
Or you may create a helper function (i.e. void addToMap(std::map <int, ValueType> &map, CSting const& v1, CString const& v2, CString const& v3) ), which will fill your map in a more convenient way.
std::map <int, CString, CString, CString> map; is illegal.
Either use a std::multimap or a std::map<int,std::vector<CString> >.
I believe this what you are looking for
std::map <int, std::list<CString> > myMap;
then you'll access myMap[0], then access each element in the returned std::list<CString>
I've created a map of vectors that looks like this:
map<string, vector<char> > myMap;
string key = "myKey";
vector<char> myVector;
myMap[key] = myVector;
I want to be able to append 'char's' to the vector in the map but I can't figure out how to access said vector to append once the particular key/value(vector) has been created. Any suggestions? I'm iterating over char's and might be adding a lot to the vector as I go so it would be nice to have a simple way to do it. Thanks.
I would like the vector in map to be appended as I go. I don't need the original vector...I just need to return the map of key/vector's that I've created (after apending) so that I can pass it to another function. What does the * in map* > do? Is that refrencing a pointer? (I haven't gotten there in lecture yet) Also, do I need:
myMap[key]->push_back('s');
or
myMap[key].push_back('s');
??
To append:
myMap[key].push_back('c');
Or use myMap.find, but then you have to check whether you get an end iterator. operator[] returns a reference to the vector.
But this modifies the vector stored in the map, not the original one, since you've stored a copy in the map with myMap[key] = myVector;. If that's not what you want, you should rethink your design and maybe store (smart) pointers to vectors in your map.
Given you know the key:
string key = "something";
char ch = 'a'; // the character you want to append
map<string, vector<char> >::iterator itr = myMap.find(key);
if(itr != myMap.end())
{
vector<char> &v = itr->second;
v.push_back(ch);
}
you could also use the map::operator[] to access the map entry, but if the key does not exist, a new entry with that key will be created:
vector<char> &v = myMap[key]; // a map entry will be created if key does not exist
v.push_back(ch);
or simply:
myMap[key].push_back(ch);
To access the mapped value, which in your case is a vector, you just supply the key in square brackets like you did to assign the value.
So, to append 'a':
myMap[key].push_back('a');
I have an new suggestion. You can use vector<char>* instead of vector<char> in order to collect pointer of vectors in your map. For more information see the bellow code:
map<string, vector<char>* > myMap;
string key = "myKey";
vector<char>* myVector = new vector<char>();
myMap[key] = myVector;
myMap[key]->push_back('S');