memsetC++ 中的向量是否有任何等价函数?
(不是clear()或erase()方法,我想保留向量的大小,我只想初始化所有值.)
他们的目标都是一样的:找到类似的向量.你在哪种情况下使用哪种?(任何实际例子?)
第一个例子:
int main(){
using namespace std;
vector<int> v1{10, 20, 30, 40, 50};
vector<int> v2{10, 20, 30, 40, 50};
if(v1==v2)
cout<<"equal";
else
cout<<"unequal";
} // it returns equal
Run Code Online (Sandbox Code Playgroud)
第二个例子:
int main(){
using namespace std;
vector<int> v1{10, 20, 30, 40, 50};
vector<int> v2{10, 20, 100000, 40, 50};
if(v1==v2)
cout<<"equal";
else
cout<<"unequal";
} // it returns notequal
Run Code Online (Sandbox Code Playgroud) 我有以下一批代码:
std::vector<std::unique_ptr<AVLTree_GeeksforGeeks>> AVLArray(100000);
/* Let's add some objects in the vector */
AVLTree_GeeksforGeeks *avl = new AVLTree_GeeksforGeeks();
avl->Insert[2]; avl->Insert[5]; AVL->Insert[0];
unique_ptr<AVLTree_GeeksforGeeks> unique_p(avl);
AVLArray[0] = move(unique_p);
/* we do this for a number of other trees, let's say another 9...
...
...
Now the vector has objects up until AVLTree[9] */
/* Let's try iterating through its valid, filled positions */
for(auto i : AVLTree )
{
cout << "Hey there!\n"; //This loop should print 10 "Hey there"s.
}
Run Code Online (Sandbox Code Playgroud)
茹罗.最后一部分,for()循环中的编译错误. …
如何删除动物园系列的最后100个元素?
我知道名字[-element]符号,但我不能让它减去一个完整的部分
iterator insert ( iterator position, const T& x );
Run Code Online (Sandbox Code Playgroud)
是std::Vector类的insert运算符的函数声明.
此函数的返回类型是指向插入元素的迭代器.我的问题是,给定这种返回类型,最有效的方法是什么(这是我正在运行的一个更大的程序的一部分,其中速度是最重要的,所以我正在寻找最有效的计算方式)在开头插入.是以下吗?
//Code 1
vector<int> intvector;
vector<int>::iterator it;
it = myvector.begin();
for(int i = 1; i <= 100000; i++){
it = intvector.insert(it,i);
}
Run Code Online (Sandbox Code Playgroud)
要么,
//Code 2
vector<int> intvector;
for(int i = 1; i <= 100000; i++){
intvector.insert(intvector.begin(),i);
}
Run Code Online (Sandbox Code Playgroud)
基本上,在代码2中,是参数,
intvector.begin()
Run Code Online (Sandbox Code Playgroud)
与在代码1中使用返回的迭代器相比,计算评估的"成本"还是应该同样便宜/昂贵?
例如,我有一个向量[1, 2, 3],我想更新第二个元素,以便向量变为[1, 5, 3].在其他语言中,我会做类似的事情array[1] = 5,但我不知道任何可以让我在Clojure中轻松完成的事情.
关于如何实现这一点的想法,或者我是否应该使用不同的数据结构?
我有一个结构矢量vec.这种结构具有元素int a,int b,int c.我想从向量中的最后一个结构赋予一些int var元素c.请问您能提供这个简单的解决方案吗?我要像这样排成一行:
var = vec.end().c;
Run Code Online (Sandbox Code Playgroud) std::vector<int> a;
std::vector<int> b;
std::vector<int> c;
Run Code Online (Sandbox Code Playgroud)
我想通过附加b's和c's元素来连接这三个向量a.哪种方法最好,为什么?
1)使用vector::insert:
a.reserve(a.size() + b.size() + c.size());
a.insert(a.end(), b.begin(), b.end());
a.insert(a.end(), c.begin(), c.end());
b.clear();
c.clear();
Run Code Online (Sandbox Code Playgroud)
2)使用std::copy:
a.reserve(a.size() + b.size() + c.size());
std::copy(b.begin(), b.end(), std::inserter(a, a.end()));
std::copy(c.begin(), c.end(), std::inserter(a, a.end()));
b.clear();
c.clear();
Run Code Online (Sandbox Code Playgroud)
3)使用std::move(from C++11):
a.reserve(a.size() + b.size() + c.size());
std::move(b.begin(), b.end(), std::inserter(a, a.end()));
std::move(c.begin(), c.end(), std::inserter(a, a.end()));
b.clear();
c.clear();
Run Code Online (Sandbox Code Playgroud) 在C++ 98中,std::vector填充构造函数的原型具有初始化程序的默认值.
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
Run Code Online (Sandbox Code Playgroud)
C++ 11使用两个原型.
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
Run Code Online (Sandbox Code Playgroud)
(在C++ 14中,填充构造函数再次改变,但这不是这个问题的重点.)
参考链接在这里.
为什么C++ 11会弃用默认的初始化值value_type()?
顺便说一句,我尝试编译以下代码clang++ -std=c++11并发出错误,这意味着值类型仍然需要有一个默认构造函数S() {},即默认构造.
#include <vector>
struct S {
int k;
S(int k) : k(k) {} // intentionally remove the synthesized default constructor
};
int main() {
std::vector<S> s(5); // error: no matching …Run Code Online (Sandbox Code Playgroud)