我正在尝试以下代码:
struct _Struct2
{
void *ptr;
double dval;
};
struct _Struct
{
float fval;
int ival;
std::vector<_Struct2> data;
};
std::vector<_Struct> vec;
int main()
{
vec.resize( 9 );
for ( int i = 0; i < vec.size(); i++ )
{
_Struct &elem = vec[i];
int len = elem.data.size(); // elem.data is [0]()
}
}
Run Code Online (Sandbox Code Playgroud)
resize(9)应该分配9个_Struct类型的元素.而且,事实上它是有效的.但是_Struct类型的每个元素都没有被初始化,尤其是数据元素,这是另一个std :: vector.我希望它被初始化为空的std :: vector.必须手动完成吗?我认为resize()方法会调用每个_Struct元素的默认构造函数.谢谢
PS.这里使用的结构名称只是我想到的第一件事.这只是一个例子.我的Visual Studio告诉我elem.data在调试视图中对应于[0]().
PS.忘了[0]().
我正在阅读C++ Primer,3rd Ed(Lippman和Lajoie),并且它说当需要重新分配向量以便为添加的更多元素腾出空间时push_back(),元素在新空间中被复制构造然后是析构函数被称为旧元素.我很困惑为什么这是必要的 - 为什么数据不能一点一点地复制?我假设答案与动态内存分配有关,但我目前的推理方法是,即使向量元素处理动态内存,实际存储在元素中的数据也将是指针,这意味着按位复制将保留它们指向的位置并且不会出现任何问题.我可以看到如何重新定位元素指向的动态分配的内存将是一个问题,因为它会使指针无效,但据我所知,向量重新定位没有理由这样做.
有人能给我一个不应该逐位移动的类的简单例子吗?
使用安全吗?
vector.emplace_back( new MyPointer() );
Run Code Online (Sandbox Code Playgroud)
或者,向量中抛出异常或某些失败会导致内存泄漏吗?
是否更好地执行以下某种形式,首先将指针放在临时的unique_ptr中.
vector.emplace_back( std::unique_ptr<MyPointer>( new MyPointer() ) );
Run Code Online (Sandbox Code Playgroud)
因此,如果发生向量故障,临时unique_ptr仍将清理内存?
在使用一些遗留代码的项目中工作时,我发现了这个功能:
std::vector<std::string> Object::getTypes(){
static std::string types [] = {"type1","type2", "type3"};
return std::vector<std::string> (types , types +2);
}
Run Code Online (Sandbox Code Playgroud)
我可能会把它写成:
std::vector<std::string> Object::getTypes(){
std::vector<std::string> types;
types.push_back("type1");
types.push_back("type2");
types.push_back("type3");
return types;
}
Run Code Online (Sandbox Code Playgroud)
这仅仅是一种风格选择还是我缺少的东西?任何帮助将不胜感激.对不起,如果这太基础了.
更新: 实际上发现覆盖相同方法的不同类可以这样或那样做,所以它更加含糊不清.我会让它们都一样,但如果有的话,我会更喜欢更好的方法.
编辑
请注意,上面的遗留代码不正确,因为它只使用数组的前两个元素初始化向量.但是,此错误已在评论中讨论过,因此应予以保留.
正确的初始化应该如下所示:
...
return std::vector<std::string> (types, types + 3);
...
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法(更快或代码更少的符号)比擦除元素并重新添加到后面?
template <typename T>
void moveItemToBack(std::vector<T>& v, size_t itemIndex)
{
T tmp(v[itemIndex]);
v.erase(v.begin() + itemIndex);
v.push_back(tmp);
}
Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么我没有得到相同的输出?
main.cpp中:
#include <iostream>
#include <vector>
using namespace std;
struct Cell
{
vector<int> vtx;
};
int main()
{
vector <Cell> cells;
Cell tmp;
tmp.vtx.reserve(5);
cells.push_back (tmp);
cout << tmp.vtx.capacity() << endl;
cout << cells[0].vtx.capacity() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
5
0
Run Code Online (Sandbox Code Playgroud) 请问vector<T, std::allocator<T>>::clear()是O(1),如果T是平凡破坏?
gcc在bits/stl_vector.hcalls std::_Destroy(bits/stl_construct.h)中的实现.这种实现方式可以优化T通过标记调度来简单地破坏的情况std::is_trivially_destructible<T>.
通过llvm(3.5.0)实现,vector::clear调用std::allocator<T>::destroy每个元素,然后调用析构函数.
_LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
Run Code Online (Sandbox Code Playgroud)
这最终会vector::clear() O(1)在libc ++中进行优化吗?
所以我试图在我的矢量背面添加整数,并错误地认为push_back()将新数据添加到矢量的前面(又名矢量[0]).我做了一个测试在Xcode和测试push_back()反对emplace_back(),并得到了相同的结果.我认为他们是不同的,但这让我觉得他们可能会做同样的事情.如果是这样,为什么矢量有不同的方法?
这是我的代码,以防我正在做的事情:
#include <vector>
#include <iostream>
using namespace std ;
int main(int argc, const char * argv[])
{
// for push_back
vector<int> push;
push.push_back(1);
push.push_back(2);
push.push_back(3);
push.push_back(4);
push.push_back(5);
push.push_back(6);
//display push_back
for (int i = 0; i < push.size(); i++) {
cout << "push[" << i << "]: " << push[i] << endl;
}
// distance between the two funcitons
cout << endl << endl;
vector<int> emplace;
emplace.emplace_back(1);
emplace.emplace_back(2);
emplace.emplace_back(3);
emplace.emplace_back(4);
emplace.emplace_back(5);
emplace.emplace_back(6);
//display …Run Code Online (Sandbox Code Playgroud) 我的问题是我有一个包含std :: ifstream,std :: ofstream和std :: mutex的类.这些对象都不能直接复制,如下例所示.
std::ifstream stream1;
std::ifstream stream2;
stream1 = stream2; //<-Compiler Error!
Run Code Online (Sandbox Code Playgroud)
我的问题不是我要复制我的类的任何实例,而是vector中的push_back()函数试图为我的类调用复制构造函数.我设计了一个复制我的问题并将其粘贴在下面的示例.
#include <fstream> //for std::ifstream // std::ofstream
#include <vector> //for std::vector
#include <mutex> //for std::mutex
class MyClass
{
public:
MyClass(int ID) : ID(ID) { }
std::ofstream outputstream;
std::ifstream inputstream;
std::mutex mymutex;
private:
int ID;
};
int main()
{
std::vector<MyClass> MyVector;
MyVector.push_back(MyClass(1)); //<-- Error C2280 'MyClass::MyClass(const MyClass &)': attempting to reference a deleted function
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在试图弄清楚如何解决这个错误时遇到了问题.任何帮助将不胜感激.请记住,我永远不会直接调用复制构造函数,因为我在实际场景中不需要复制此类的实例,但似乎push_back正在调用它并且我在尝试覆盖它时失败了.
编辑:我认为解决它的一种方法是使用指针而不是ifstream,ofstream和mutex对象,但我宁愿不惜一切代价避免这种情况.
在这个例子中,比较两个向量与<operator result in operator <,在Integer类上定义,为每个元素调用两次.但是,将两个向量与==运算符进行比较时不会发生这种情况.
#include<iostream>
#include<vector>
class Integer {
public:
Integer(int value) : m_value(value) {}
friend bool operator<(const Integer& lhs, const Integer& rhs);
friend bool operator==(const Integer& lhs, const Integer& rhs);
private:
int m_value;
};
bool operator<(const Integer& lhs, const Integer& rhs) {
std::cout << lhs.m_value << " < " << rhs.m_value << '\n';
return lhs.m_value < rhs.m_value;
}
bool operator==(const Integer& lhs, const Integer& rhs) {
std::cout << lhs.m_value << " == " << rhs.m_value << '\n';
return …Run Code Online (Sandbox Code Playgroud)