我编写了以下代码来理解移动语义.它在g ++ - 4.6中按预期工作(即没有副本,只有移动)但在g ++ - 4.7.0中没有.我认为这是链接在g ++ - 4.7.0中的一个错误,但是这个链接说它不是g ++ - 4.7中的错误.所以,正如我从上面的链接中所理解的那样,我使移动构造函数无法生成,但它仍然只复制.但是,如果我将复制构造函数设为nothrow,则只进行移动.任何人都能解释一下吗?
#include <iostream>
#include <vector>
using namespace std;
struct S{
int v;
static int ccount, mcount;
S(){}
//no throw constructor
//S(nothrow)(const S & x){
S(const S & x){
v = x.v;
S::ccount++;
}
S(S&& x){
v = x.v;
S::mcount++;
}
};
int S::ccount = 0;
int S::mcount = 0;
int main(){
vector<S> v;
S s;
for(int i = 0; i < 10; i++) {
v.push_back(std::move(s));
} …Run Code Online (Sandbox Code Playgroud) 我有一个循环,我使用一个函数将std :: unique_ptr返回给抽象类的对象.我想通过push_back将这些对象存储到std :: vector中.但由于对象是抽象类型,我得到以下错误:
error: cannot allocate an object of abstract type
Run Code Online (Sandbox Code Playgroud)
为线
cells.push_back(std::move(*cell));
Run Code Online (Sandbox Code Playgroud)
其中,cell是std::vector抽象类型的类型,并且cell是类型
std::unique_ptr<AbstractType>&& cell
Run Code Online (Sandbox Code Playgroud)
(我实际上传递cell给一个处理程序类)我知道一个人不能实例化一个抽象类型,因为我理解std:move操作符需要以某种方式实例化对象?
任何人都可以帮我解决问题吗?或者函数(不是我的项目部分)是否应该返回一个指向抽象类型对象的唯一指针?
我有一个声明为的向量
std::vector<int> MyVector;
MyVector.push_back(5);
MyVector.push_back(6);
MyVector.push_back(7);
Run Code Online (Sandbox Code Playgroud)
我应该如何在for循环中使用它?
通过迭代器迭代它?
for (std::vector<int>::iterator it=MyVector.begin(); it!=MyVector.end(); ++it)
{
std::cout << "Vector element (*it): " << *it << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
或者通过它的访问迭代器?
for (std::vector<int>::size_type i=0; i<MyVector.size(); i++)
{
std::cout << "Vector element (i) : " << MyVector.at(i) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在我在互联网上找到的例子中都使用了它们.在所有条件下,其中一个优于另一个吗?如果没有,我什么时候应该更喜欢其中一个呢?
为什么以下代码对bool的std :: vector不起作用(如所考虑的)?bool-vector中的一些元素是随机错误的.对于int向量,所有工作正常(使用多于3个循环进行测试).
我在ubuntu 14.04 64bit上使用g ++ 4.8.4.
#include <iostream>
#include <vector>
using namespace std;
class TestBool
{
public:
TestBool() {}
bool test() {return true;}
int testInt() {return 10;}
};
void testBVec(vector<bool> &bv, size_t loop)
{
cout << "boolvec loop " << loop << endl;
for(size_t i = 0; i < bv.size(); i++) {
if( ! bv[i])
cout << "wholy shit with bool at index " << i << endl;
}
}
void testIntVec(vector<int> &iv, size_t loop)
{
cout << …Run Code Online (Sandbox Code Playgroud) C++ 14最终工作草案做出如下评论std::vector:
存储管理是自动处理的,但可以提供提示以提高效率.
矢量的存储自动处理,根据需要进行扩展和收缩.
而动态数组的维基百科条目说:
C++
std::vector和Ruststd::vec::Vec是动态数组的实现
所以我认为当一个向量的容量远大于它的大小时,它应该自动减少它的容量.我写了下面的代码来检查我的假设:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {};
cout << "initialization" << endl;
cout << " capacity: " << v.capacity() << endl;
cout << " size: " << v.size() << endl;
for (int i = 1; i <= 10000; i++)
v.push_back(i);
cout << "after inserting a lot of elements" << endl;
cout << " capacity: " …Run Code Online (Sandbox Code Playgroud) 晚上好.
我知道C风格的数组或std :: array并不比矢量快.我一直使用矢量(我使用它们很好).但是,我有一些情况,使用std :: array比使用std :: vector更好,我不知道为什么(用clang 7.0和gcc 8.2测试).
让我分享一个简单的代码:
#include <vector>
#include <array>
// some size constant
const size_t N = 100;
// some vectors and arrays
using vec = std::vector<double>;
using arr = std::array<double,3>;
// arrays are constructed faster here due to known size, but it is irrelevant
const vec v1 {1.0,-1.0,1.0};
const vec v2 {1.0,2.0,1.0};
const arr a1 {1.0,-1.0,1.0};
const arr a2 {1.0,2.0,1.0};
// vector to store combinations of vectors or arrays
std::vector<double> glob(N,0.0);
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.初始化变量的上述代码不包含在基准测试中.现在,让我们写一个函数元素(结合double …
我在向量中插入数据时收到分段错误。我认为载体没有分配。
我不想reserve大小。怎么做?
class A
{
private:
struct data
{
int x;
int y;
};
std::vector<data>Set;
public:
void insert()
{
Set[0].x = 5; Set[0].y = 6;
}
};
A a;
a.insert(); // Segmentation Fault
Run Code Online (Sandbox Code Playgroud) 我正在使用一些代码,通过将其地址与描述数据std::vector范围的地址进行比较,检查是否在恒定时间内包含给定元素。vector然而我怀疑,虽然它有效,但它依赖于未定义的行为。如果该元素不包含在 中,vector则不允许进行指针比较。
bool contains(const std::vector<T>& v, const T& a) {
return (v.data() <= &a) && (&a < v.data() + v.size());
}
Run Code Online (Sandbox Code Playgroud)
我相信这是未定义的行为吗?如果是这样,有没有办法在不大幅改变代码时间复杂度的情况下做同样的事情?
我有以下代码:
#include <vector>
#include <iostream>
std::vector <int> a;
int append(){
a.emplace_back(0);
return 10;
}
int main(){
a = {0};
a[0] = append();
std::cout << a[0] << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该函数append()的副作用是将向量大小增加一。由于向量的工作方式,当超出其容量时,可能会触发其内存的重新分配。
因此,在执行 时a[0] = append(),如果发生重新分配,则 thena[0]无效并指向向量的旧内存。因此,您可以预期向量最终会变为,{0, 0}而不是{10, 0},因为它分配给旧向量a[0]而不是新向量。
让我感到困惑的是,这种行为在 C++14 和 C++17 之间发生了变化。
在 C++14 上,程序将打印 0。在 C++17 上,它将打印 10,这意味着a[0]实际上分配了 10。所以,我有以下问题,但我找不到答案:
a[0]在评估赋值表达式的 RHS 之后评估 的内存地址?C++14 之前是否对此进行了评估,这就是它发生变化的原因?https://en.cppreference.com/w/cpp/container/vector说:
矢量的存储是自动处理的,根据需要扩展和收缩。
但我找不到“根据需要”发生收缩的情况。任何收缩操作都不会收缩 std::vector 的容量:
清除():
保持向量的capacity()不变
当调整到更小的尺寸时,向量容量永远不会减少,因为这会使所有迭代器无效
擦除():
在擦除点或擦除点之后使迭代器和引用无效,包括 end() 迭代器。
这意味着擦除点之前的迭代器和引用不会失效,如果容量更改则不会出现这种情况。
迭代器和对最后一个元素的引用以及 end() 迭代器均无效。
这同样适用于此。
Shrink_to_fit() 确实收缩了 std::vector,但它是在 C++11 中添加的,并且上面的句子(“并且收缩”)没有标记为仅适用于 C++11 及更高版本。此外,对shrink_to_fit()的显式调用不是自动的(“矢量的存储是自动处理的”)并且不会“根据需要”发生。
那么 std::vector 何时自动收缩其存储空间呢?