开发者

Data in Vectors

开发者 https://www.devze.com 2023-02-24 09:11 出处:网络
I have 2 vectors both of different types i.e. 1. std::vector<project3::Vertex<VertexType, EdgeType>> Vertice2; //Contains a list of Vertices

I have 2 vectors both of different types i.e.

1. std::vector<project3::Vertex<VertexType, EdgeType>> Vertice2; //Contains a list of Vertices
2. std::vector<std::string>temp12;

My requirement is I want to store all the data from Vertice2 to temp12. Tried out a lot many different ways, but getting error. Even type casting didn't work out for me.

Latest I tried was temp.assign(g1.Vertice2.begin(), g1.Vertice2.end());

Error: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'project3::Vertex&l开发者_StackOverflow社区t;VertexType,EdgeType>' to 'const std::basic_string<_Elem,_Traits,_Ax> &'   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory  208 1   Project_3_Revised


You have a vector of apples that you're trying to store in a vector of oranges. But apples aren't oranges, and that's your basic problem.

Either you need to make temp a vector<Vertex...>, or you need to convert each Vertex object to a string, and then store the resulting strings. If you are trying to cram a Vertex in to a vector<string> without converting it, give it up. You can't and should not even try to do this. You're trying to put a battleship in to a pencil cup.

If you go with conversion, then using std::transform along with a conversion function of your own devising is a pretty straightforward way to do this.

Psudocode follows:

std::string ConvertVertexToString(const Vertex& vx)
{
  std::stringstream ss;
  ss << vx.prop_a << " " << vx.prop_b;
  return ss.str();
}

int main()
{
  ...

  std::transform(Vertice2.begin(), Vertice2.end(), back_inserter(temp12), &ConvertVertexToString);

}


C++ does not provide any default casts to std::string. C++'s templates are strongly typed, like the rest of the language.

You need to create a method or function to convert a project3:Vertex into a std::string.

Once you have that, you can use C++'s transform function.

std::transform(Vertice2.begin(), Vertice2.end(), temp12.begin(), my_function_to_make_strings);


C++ does not support arbitrarily assigning objects of different types. In most cases casting won't work either and even if you force it to work (like a <reinterpret_cast>) it is not safe to do.

Your best option would be to use operator overloading and copy constructors to explicitly define the copying behavior you are expecting from your objects. For example, it is not clear when you assign a vertex to a string, what data elements should be copied.


Your basic problem is that you have project3::Vertex<VertexType, EdgeType>, and you want std::string. So how do you convert one to the other?

The usual solution for converting to a string of characters (std::string or other) is to overload the << operator. So you need first to define a function

std::ostream&
operator<<(std::ostream& dest,
           project3::Vertex<VertexType, EdgeType> const& value)

This will define what you data look like when converted to a string. Once you have that, something like:

std::transform(
    Vertice2.begin(), Vertice2.end(),
    std::back_inserter(temp12),
    (std::string (*)(
        project3::Vertex<VertexType, EdgeType> const&)) boost::lexical_cast);

should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号