如何将重复的元素插入向量? [英] How to insert a duplicate element into a vector?

查看:98
本文介绍了如何将重复的元素插入向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入一个现有的向量元素的副本,将其加倍。以下代码在以前的版本中工作,但在Visual Studio 2010中失败。

  #include< iostream& 
#include< vector>

using namespace std;

int main(int argc,char * argv [])
{
vector< int&测试;
test.push_back(1);
test.push_back(2);
test.insert(test.begin(),test [0]);
cout<< test [0]<< <测试[1]<< < test [2]<< endl;
return 0;
}

输出为 -17891602 1 2 ,预期 1 1 2



我已经知道为什么会发生 - 重新分配,引用在复制到插入点之前变得无效。较旧的Visual Studio显然是以不同的顺序执行的,从而证明未定义行为的一个可能的结果是正确工作,并且证明它不是你应该依赖的东西。



我想出了两种不同的方法来解决这个问题。一个是使用 reserve 确保不会发生重新分配:

  test.reserve(test.size()+ 1); 
test.insert(test.begin(),test [0]);

另一种是从引用中复制一个副本,

 模板< typename T> 
T make_copy(const T& original)
{
return original;
}

test.insert(test.begin(),make_copy(test [0]));

虽然两者都工作,但没有一种感觉像一个自然的解决方案。是否有我遗漏的东西?

解决方案

问题是 vector :: insert 引用一个值作为第二个参数,而不是一个值。你不需要模板来复制,只需使用复制构造函数创建另一个对象,这将通过引用传递。即使调整向量的大小,此副本仍然有效。

  #include< iostream> 
#include< vector>

using namespace std;

int main(int argc,char * argv [])
{
vector< int&测试;
test.push_back(1);
test.push_back(2);
test.insert(test.begin(),int(test [0]));
cout<< test [0]<< <测试[1]<< < test [2]<< endl;
return 0;
}


I'm trying to insert a copy of an existing vector element to double it up. The following code worked in previous versions but fails in Visual Studio 2010.

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
   vector<int> test;
   test.push_back(1);
   test.push_back(2);
   test.insert(test.begin(), test[0]);
   cout << test[0] << " " << test[1] << " " << test[2] << endl;
   return 0;
}

Output is -17891602 1 2, expected 1 1 2.

I've figured out why it's happening - the vector is being reallocated, and the reference becomes invalid before it's copied to the insertion point. The older Visual Studio apparently did things in a different order, thus proving that one possible outcome of undefined behavior is to work correctly and also proving that it's never something you should rely on.

I've come up with two different ways to fix this problem. One is to use reserve to make sure that no reallocation takes place:

   test.reserve(test.size() + 1);
   test.insert(test.begin(), test[0]);

The other is to make a copy from the reference so that there's no dependency on the reference remaining valid:

template<typename T>
T make_copy(const T & original)
{
    return original;
}

   test.insert(test.begin(), make_copy(test[0]));

Although both work, neither one feels like a natural solution. Is there something I'm missing?

解决方案

The issue is that vector::insert takes a reference to a value as the second parameter and not a value. You don't need the template to make a copy, just use a copy constructor to create another object, which will be pass by reference. This copy remains valid even if the vector is resized.

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
   vector<int> test;
   test.push_back(1);
   test.push_back(2);
   test.insert(test.begin(), int(test[0]));
   cout << test[0] << " " << test[1] << " " << test[2] << endl;
   return 0;
}

这篇关于如何将重复的元素插入向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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