Answered by:
how to insert vector object into the map using C++

Question
-
Hello All,
My requirement is to insert vector object into the map. But, when trying to insert vector object into the map getting the below error.
no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::insert [with _Kty=std::wstring, _Ty=std::wstring, _Pr=std::less<std::wstring>, _Alloc=std::allocator<std::pair<const std::wstring, std::wstring>>]" matches the argument list
Could someone help me how to insert vector object into the map?
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <map>
using namespace std;
int main()
{
std::vector<std::wstring> Locations;
std::map<std::wstring, std::wstring> m_params;m_params.insert({ L"emailType" , L"email" });
m_params.insert({ L"emailLocation" , Locations }); //Here I am getting an error
for (auto itr = m_params.begin(); itr != m_params.end(); ++itr)
{
}
return 0;
}
Answers
-
std::map<std::wstring, std::wstring> m_params;
This map object is declared to hold strings, not vectors.
For example -
#include <string> #include <vector> #include <map> #include <iterator> #include <iostream> using namespace std; int main() { map<wstring, vector<wstring>> m_params; m_params.insert({ L"vec1", {L"one", L"two"} }); m_params.insert({ L"vec2", {L"three", L"four"} }); for (auto& i : m_params) { wcout << L"Key: " << i.first << endl; for (auto& e : i.second) wcout << L"Vector element: " << e << endl; } return 0; }
- Edited by RLWA32 Monday, December 2, 2019 5:33 PM added example
- Marked as answer by John paul coder Tuesday, December 3, 2019 4:39 PM
-
Hello,
if you only want to add the vector to your map, insert each item of the vector.
for (std::vector<std::wstring>::iterator it = Locations.begin(); it != Locations.end(); ++it) { m_params.insert({ L"emailLocation" , *it }); }
Regards, Guido
- Edited by Guido Franzke Tuesday, December 3, 2019 7:41 AM
- Marked as answer by John paul coder Tuesday, December 3, 2019 4:39 PM
All replies
-
std::map<std::wstring, std::wstring> m_params;
This map object is declared to hold strings, not vectors.
For example -
#include <string> #include <vector> #include <map> #include <iterator> #include <iostream> using namespace std; int main() { map<wstring, vector<wstring>> m_params; m_params.insert({ L"vec1", {L"one", L"two"} }); m_params.insert({ L"vec2", {L"three", L"four"} }); for (auto& i : m_params) { wcout << L"Key: " << i.first << endl; for (auto& e : i.second) wcout << L"Vector element: " << e << endl; } return 0; }
- Edited by RLWA32 Monday, December 2, 2019 5:33 PM added example
- Marked as answer by John paul coder Tuesday, December 3, 2019 4:39 PM
-
-
Hello,
if you only want to add the vector to your map, insert each item of the vector.
for (std::vector<std::wstring>::iterator it = Locations.begin(); it != Locations.end(); ++it) { m_params.insert({ L"emailLocation" , *it }); }
Regards, Guido
- Edited by Guido Franzke Tuesday, December 3, 2019 7:41 AM
- Marked as answer by John paul coder Tuesday, December 3, 2019 4:39 PM