vector in einer map abspeicher
-
Hallo,
ist es möglich einen Vector in einer Map abzuspeichern? Mein Kompiler bringt da tonnenweise Fehlermeldungen...
mfg flowni
mein Beispielcode...
std::map<MyClass , std::vector<MyClass> > map_myMap; ... MyClass o_test; MyClass o_test1; std::vector<MyClass> v_myVector; v_myVector.push_back(o_test1); map_myMap[o_test] = v_myVector;
-
Und wie lauten die Fehlermeldungen? Soweit ich das sehe ist der Code korrekt. Hat MyClass vielleicht keinen Copy-Constructor?
-
Kopierkonstruktor habe ich keinen, da ich nur Float- Attribute in der Klasse habe...
Gruss
/usr/include/c++/3.3.5/bits/stl_tree.h:1323: instantiated from `std::_Rb_tree_iterator<_Val, _Val&, _Val*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::lower_bound(const _Key&) [with _Key = N_VRFParser::FinishElement, _Val = std::pair<const N_VRFParser::FinishElement, std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> > >, _KeyOfValue = std::_Select1st<std::pair<const N_VRFParser::FinishElement, std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> > > >, _Compare = std::less<N_VRFParser::FinishElement>, _Alloc = std::allocator<std::pair<const N_VRFParser::FinishElement, std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> > > >]' /usr/include/c++/3.3.5/bits/stl_map.h:508: instantiated from `typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::lower_bound(const _Key&) [with _Key = N_VRFParser::FinishElement, _Tp = std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> >, _Compare = std::less<N_VRFParser::FinishElement>, _Alloc = std::allocator<std::pair<const N_VRFParser::FinishElement, std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> > > >]' /usr/include/c++/3.3.5/bits/stl_map.h:316: instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = N_VRFParser::FinishElement, _Tp = std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> >, _Compare = std::less<N_VRFParser::FinishElement>, _Alloc = std::allocator<std::pair<const N_VRFParser::FinishElement, std::vector<N_VRFParser::FinishElement, std::allocator<N_VRFParser::FinishElement> > > >]' /root/user/zem/workspace/UNT/cobusiness/VRFParser/VRFParserReadFile.cpp:879: instantiated from here /usr/include/c++/3.3.5/bits/stl_function.h:197: error: no match for 'operator<' in '__x < __y'
-
error: no match for 'operator<' in '__x < __y'du benötigst einen überladenen operator < für deine klasse da map sortiert speichern möchte oder du änderst den _Compare typ
-
Vielen Dank für die schnelle Antwort aber könntest du mir das schnell erklären warum genau der nun einen überladenen Operator braucht und was hat das mit dem Vergleichmechanismus der map zu tun.
Blicke da nicht so genau durch.Vielen Dank
-
Maps werden immer nach ihrem Schlüsseltyp sortiert. Damit das funktioniert, brauchst du ein Vergleichskriterium.
Standardmäßig ist der Comparetyp std::less<KeyType> und std::less ist mit dem <-Operator definiert.
Du hast also 3 Möglichkeiten, den Code zum Laufen zu bringen:
1. Du schreibst einen <-Operator für MyClass (ist aber nur sinnvoll, wenn ein solcher Vergleich tatsächlich logisch möglich ist)
2. Du spezialisierst das std::less-Template für deine Klasse
3. Du schreibst eine eigene Vergleichsfunktion (das Sinnvollste, wenn du nur in einem Kontext eine solche Map anlegst.
-
Vielen Dank für alles, nun funkt es endlich

Gruss flownfluid