Hi all, I am writing a program that implements a uni-directional graph using std::map. I have to search for the key and then copy the vector<string> values corresponding to that key into a new location in the map. The problem is that the key I search for is extracted from yet another location in the map. I understand why I am getting the error as std::copy_n does not copy from vector<string> to string (although it works the other way) but I don't know how to fix this.
I am copying a snippet of the code below:
The keys to search for are stored in a vector<string> corresponding to key mykey.
for (vector<string>::iterator itr =mymap.find(mykey)->second.begin(); itr!=mymap.find(mykey)->second.end();itr++){
string find_key="";
std::copy_n(itr,1,find_key.begin());//error C2664 :cannot convert parameter 1 from 'std::_Vector_iterator<_Myvec>' //to 'const std::basic_string<_Elem,_Traits,_Ax> &'
if (mymap.find(find_key)==mymap.end())
cout<<"key not found"<<endl;
else
mymap[mystring].insert(mymap.find(find_key)->second.begin(), mymap.find(find_key)->second.end());}
What are you trying to do exactly? What should happen as result of your manipulations?
Are you trying to stuff a bunch of string containing in vector in a single string?
What exactly are you trying to accomplish in the context of the graph, from a high-level perspective?
std::copy_n does not copy from vector<string> to string (although it works the other way)
Are you sure std::copy_n() is doing what you think it's doing? This:
1 2 3
//std::string src;
//std::vector<std::string> dst;
std::copy_n(src.begin(), n, dst.begin());
will cause the first n elements of dst to be equal to each of the first n characters of src. It will not copy src into the first n elements. If that's what you want to do, you need std::fill().