在运行时显示可变性质向量元素的复制内容. [英] To Display copied contents of vectors elements of variable nature @ run time.

查看:65
本文介绍了在运行时显示可变性质向量元素的复制内容.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要将容器,向量结构(具有可变元素)的内容(值)动态(运行时)复制到一个空结构中,

即,它必须获取元素,前提是它具有给定测试的元素名称和相应标记,

Content1:("Michael"," 93),(" Philips," 92);

Content2:("Joseph","99"),("Ahom","76"),("Lincoln","90");

内容3:........................

........
内容N:

所有这些内容都是向量形式的,我正在将其复制到空向量(单个向量)中,它可以在运行时包含/显示所有值.

在这里,需要遍历每个内容(具有可变性质),并且contentsN:是可变的,一种方法如何可以相同,按照这种顺序,什么是最好的策略才能显示动态复制的所有元素,直到null. >
我正在使用C ++,Windows 7 O/S,VS2008 IDE

在此先感谢...

问候,
VishalK_99

Hi,

I need to copy contents(values) from container, vector struct(having variable elements) into an empty struct dynamically(runtime),

ie., It has to fetch the elements assume it has elements Name & corresponding marks for given tests,

Content1 : ("Michael", ""93"), ("Philips", "92");

Content2: ("Joseph", "99"), ("Ahom", "76") ,("Lincoln" , "90");

Content 3: ........................

........
Content N:

All these contents are of vectors form, I am copying the same to empty vector(single vector), which could contain/display all the values at runtime.

Here, each contents needs to be traversed(of variable nature) and contentsN: is variable, How could one approach the same, In that order, what is the best strategy in order to display all the elements dynamically copied uptill null.

I am working on C++, Windows 7 O/S, VS2008 IDE

Thanks in Advance...

With Regards,
VishalK_99

推荐答案

嗯,有两种方法可以遍历向量中的(未知数量)项.

1)您可以使用.size()成员函数获得编号项,然后使用数组符号[]和范围为[0..size-1]的索引访问元素.
2)您可以在for循环中使用迭代器.


我使用#2.

1)我刚刚制作了一个结构来保存名称和标记
2)然后我输入一个向量来保存这些结构
3)然后我键入第二个向量来保存这些向量
4)我制作了一个函数,该函数将迭代并打印#2中的元素
5)然后,我制作了一个函数,该函数将遍历#3中的每个向量,并将每个向量传递给#4

这是我使用的数据类型和函数原型.
Well, there''s two ways to iterate through the items (of unknown quantity) in a vector.

1) You can get the number items with the .size() member func, followed by accessing elements using array notation [] and an index in the range [0..size-1]

2) You can use an iterator in a for loop.


I used #2.

1) I just made a struct to hold the name and mark
2) I then typedef''d a vector to hold these structs
3) I then typedef''d a second vector to hold these vectors
4) I made a function which would iterate through and print the elements in #2
5) I then made a function that would iterate through each of the vectors in #3, passing each one to #4

Here''s the data types and function prototypes I used.
typedef struct
{
    std::string name;
    std::string mark;
} contentItem;

typedef vector<contentItem> vecContentItem;
typedef vecContentItem::iterator vecContentItemIter;

typedef vector<vecContentItem> vecContainer;
typedef vecContainer::iterator vecContainerIter;

void printContentVec(vecContentItem theList);
void printContainer(vecContainer theContainer);


将矢量复制到另一个矢量的方式是. ..

1.确保要存储的所有对象都有一个公共的基本接口类
2.在基类中定义一个clone()或create_copy()函数,并在派生类中实现它
3.编写一个指向基类对象的瘦包装器类(使用类似tr1 :: unique_ptr之类的东西).在其中定义一个复制构造函数和赋值运算符,使用在2
中定义的clone或make_copy函数对指向该对象的对象进行深层复制 4.将这些包装对象存储在向量中
5.使用std :: copy到处复制它们

我显示矢量内容的方式(即将它们复制到流中)将是...

1-3以上
4.从1开始在您的公共基类上定义一个virtual display()函数,并为所有派生类实现该函数
5.在包装类上实现一个插入操作符,该操作符调用您在4
中定义的display()函数 6.使用std :: copy和std :: ostream_iterator在std :: cout
上显示内容
这个想法是,基类和包装器类共同作用,以向量的形式有效存储不同类的对象.当包装对象被复制时,所有包裹的对象也将被复制.这也意味着您不必费心手动循环和使用指针(很多情况下,包装类的复制构造函数和赋值运算符仍然需要花一点时间).

这是一种方便的技术,要记住,当最终获得指向对象的指针时,这些对象希望通过值将不同类型的对象有效地插入向量中,但又不想弄乱它所伴随的所有低级信息. />
干杯,

The way I''d copy the vector into another vector is...

1. Make sure all the objects you want to store have a common base interface class
2. Define a clone() or create_copy() function in the base class and implement it in the derived class
3. Write a thin wrapper class that points to objects of the base class (using something like tr1::unique_ptr). In it define a copy constructor and assignment operator that does a deep copy of the object pointed to using the clone or make_copy function you defined in 2
4. Store these wrapper objects in your vector
5. Use std::copy to copy them around

The way I''d display the contents of a vector (i.e. copy them into a stream) would be...

1 - 3 as above
4. Define a virtual display() function on your common base class from 1 and implement it for all your derived classes
5. Implement an insertion operator on your wrapper class that calls through to the display() function you defined in 4
6. Use std::copy and an std::ostream_iterator to display things on std::cout

The idea is that the base class and wrapper class act together to store what are effectively objects of different classes by value in a vector. When objects of the wrapper are copied whatever''s wrapped gets copied as well. This also means you don''t have to fanny around with manual loops and playing with pointers (much, the copy constructor and assignment operator of the wrapper class still have to a bit).

This is a handy technique to remember whenever you end up with pointers to objects that you want to insert disparate types into a vector effectively by value but don''t want to mess around with all the low level guff that goes with it.

Cheers,

Ash


这篇关于在运行时显示可变性质向量元素的复制内容.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆