Lena wrote:[bccaarm Error] UnitDibocca.cpp(59): type 'std::multimap<String, std::vector<int> >' does not provide a subscript operator
The error is correct.
std::multimap does not have a subscript operator.
Lena wrote:How correct?
You should be using
std::map instead:
- Code: Select all
std::map<String, std::vector<int>> IniContent;
std::multimap allows multiple entries to have the same key value, which is why a subscript operator is not provided (you have to use
std::multimap::equal_range() to find all entries that match a given key value). That is not what you want. std::map holds entries with unique keys, and thus provides a subscript operator to access entries by key value.
Also, on a side note, since you are using a C++11 compiler, you should consider using 'auto' to help clean up your variable declarations when the compiler can deduce their types for you:
- Code: Select all
std::map<String, std::vector<int>> IniContent;
...
auto &values = IniContent[FullName]; // <-- here
Lena wrote:If I do not need automatic sorting (in multimap), which container should I use?
Have a look at
std::unordered_map:
- Code: Select all
std::unordered_map<String, std::vector<int>> IniContent;
Lena wrote:- Code: Select all
std::vector<std::pair<String, std::pair<int,int>> > MyPair;
//***
String FullName = item->Data[L"FullName"].AsString();
std::vector<std::pair<int,int>> &values = MyPair[FullName];//error
//***
[bccaarm Error] UnitDibocca.cpp(64): no viable overloaded operator[] for type 'std::vector<std::pair<String, std::pair<int, int> > >'
Correct, because the subscript operator of std::vector takes an array index, not a key value. If you want to use a vector of pairs, you will have to use the
std::find_if() algorithm to find a given pair by its key value:
- Code: Select all
using MyPair = std::pair<String, std::vector<int>>;
std::vector<MyPair> IniContent;
...
String FullName = item->Data[L"FullName"].AsString();
auto iter = std::find_if(IniContent.begin(), IniContent.end(),
[=](const MyPair &pair){ return (pair.first == FullName); });
if (iter == IniContent.end())
{
IniContent.emplace_back(FullName, std::vector<int>());
iter = IniContent.end() - 1;
}
auto &values = iter->second;
// use values as needed...