之间有什么区别?
auto x = vector<int>();
Run Code Online (Sandbox Code Playgroud)
和
vector<int> x;
Run Code Online (Sandbox Code Playgroud)
这两个声明是否相等,或者运行时复杂度是否有所不同?
C++标准似乎让有关能力的副作用通过或者没有声明
resize(n),与n < size(),或clear().
它确实作出了关于摊销成本push_back和pop_back- O(1)的声明
我可以设想一种实现通常的容量变化和CLRS算法(例如,放大时加倍,减少时减半size to < capacity()/4).(Cormen Lieserson Rivest Stein)
有没有人参考任何实施限制?
我有std::vector一些类的元素ClassA.另外,我想创建一个索引,使用a std::map<key,ClassA*>将一些键值映射到指向向量中包含的元素的指针.
当在向量的末尾添加元素(未插入)时,是否保证这些指针保持有效(并指向同一对象).即,以下代码是否正确:
std::vector<ClassA> storage;
std::map<int, ClassA*> map;
for (int i=0; i<10000; ++i) {
storage.push_back(ClassA());
map.insert(std::make_pair(storage.back().getKey(), &(storage.back()));
}
// map contains only valid pointers to the 'correct' elements of storage
Run Code Online (Sandbox Code Playgroud)
情况怎么样,如果我用std::list而不是std::vector?
使用g ++,我观察到创建一个大小为零的向量会调用向量的参数化对象类型的构造函数一次.然后它被删除.为什么会这样?
#include <iostream>
#include <vector>
using namespace std;
class s
{
public:
s() { cout << endl << "default s constructor" << endl; }
~s() { cout << endl << "default s destructor" << endl; }
};
int main()
{
vector<s> v(0);
}
Run Code Online (Sandbox Code Playgroud)
输出:
默认的构造函数
默认的析构函数
我有一些使用这样的矢量的简单函数(伪代码):
void someFunc(void) {
std::vector<std::string> contentVector;
// here are some operations on the vector
// should I call the clear() here or this could be ommited ?
contentVector.clear();
}
Run Code Online (Sandbox Code Playgroud)
我应该调用clear()还是可以省略这个?
可能重复:
如何在向量增长时强制执行移动语义?
insert,push_back和emplace(_back)可能导致的重新分配std::vector.我很困惑地看到以下代码复制元素而不是在重新分配容器时移动它们.
#include <iostream>
#include <vector>
struct foo {
int value;
explicit foo(int value) : value(value) {
std::cout << "foo(" << value << ")\n";
}
foo(foo const& other) noexcept : value(other.value) {
std::cout << "foo(foo(" << value << "))\n";
}
foo(foo&& other) noexcept : value(std::move(other.value)) {
other.value = -1;
std::cout << "foo(move(foo(" << value << "))\n";
}
~foo() {
if (value != -1)
std::cout << "~foo(" …Run Code Online (Sandbox Code Playgroud) 最后我可以使用[]运算符在python中使用std :: vector.诀窍是简单地在boost C++包装器中提供一个处理内部向量内容的容器:
#include <boost/python.hpp>
#include <vector>
class world
{
std::vector<double> myvec;
void add(double n)
{
this->myvec.push_back(n);
}
std::vector<double> show()
{
return this->myvec;
}
};
BOOST_PYTHON_MODULE(hello)
{
class_<std::vector<double> >("double_vector")
.def(vector_indexing_suite<std::vector<double> >())
;
class_<World>("World")
.def("show", &World::show)
.def("add", &World::add)
;
}
Run Code Online (Sandbox Code Playgroud)
另一个挑战是:如何将python列表转换为std :: vectors?我试图添加一个c ++类,期望std :: vector作为参数,并添加了相应的包装代码:
#include <boost/python.hpp>
#include <vector>
class world
{
std::vector<double> myvec;
void add(double n)
{
this->myvec.push_back(n);
}
void massadd(std::vector<double> ns)
{
// Append ns to this->myvec
}
std::vector<double> show()
{
return this->myvec;
}
};
BOOST_PYTHON_MODULE(hello)
{ …Run Code Online (Sandbox Code Playgroud) 根据https://en.cppreference.com/,std::vector<bool>具有类模板专业化,而std::array<bool, N>没有。不提供的原因有哪些?
c++ stdvector template-specialization class-template stdarray
假设我有一个double的std :: vector,即
std::vector<double> MyVec(N);
Run Code Online (Sandbox Code Playgroud)
在哪里N这么大,性能很重要.现在假设这MyVec是一个非平凡的向量(即它不是一个零向量,但已被某些例程修改).现在,我需要向量的否定版本:我需要-MyVec.
到目前为止,我一直在实施它
std::transform(MyVec.cbegin(),MyVec.cend(),MyVec.begin(),std::negate<double>());
Run Code Online (Sandbox Code Playgroud)
但是,实际上,我不知道这是否合情合理,或者只是我身边的超级天真.
我做得对吗?或者std :: transform在这种情况下只是一个超级慢的例程?
PS:我一直在使用BLAS和LAPACK库,但是我没有发现任何符合这种特殊需求的东西.但是,如果BLAS/LAPACK中存在比std :: transform更快的函数,我很高兴知道.