简体   繁体   English

向量中的数据

[英]Data in Vectors

I have 2 vectors both of different types ie 我有两个不同类型的向量

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. 我的要求是我要存储从Vertice2到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()); 我最近尝试过的是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<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. 您有一个要存储在橙色vector的苹果vector 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 string s. 您需要将temp设为vector<Vertex...> ,或者需要将每个Vertex对象转换为一个string ,然后存储生成的string s。 If you are trying to cram a Vertex in to a vector<string> without converting it, give it up. 如果您尝试将顶点填充到vector<string>而不进行转换,请放弃它。 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. 如果您进行转换,那么使用std::transform以及您自己设计的转换函数是一种非常简单的方法。

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 ++不提供对std :: string的任何默认强制转换。 C++'s templates are strongly typed, like the rest of the language. C ++的模板是强类型的,就像其他语言一样。

You need to create a method or function to convert a project3:Vertex into a std::string. 您需要创建一个方法或函数来将project3:Vertex转换为std :: string。

Once you have that, you can use C++'s transform function. 一旦有了这些,就可以使用C ++的transform函数。

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

C++ does not support arbitrarily assigning objects of different types. C ++不支持任意分配不同类型的对象。 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. 在大多数情况下,强制转换也不起作用,即使您强制强制转换(如<reinterpret_cast> ),也不安全。

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 . 您的基本问题是您拥有project3::Vertex<VertexType, EdgeType> ,并且您需要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. 转换为字符串( std::string或其他)的通常方法是重载<<运算符。 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. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM