草庐IT

vector_tostr

全部标签

c++ - 有没有办法为符合标准的 vector 实现插入方法?

首先,假设A是一个类型:一个可能抛出的复制构造函数/赋值运算符。没有移动构造函数/赋值。这是C++03RAII类型的常见示例。现在让我引用C++14标准(剪掉不相关的部分):§23.2.1Generalcontainerrequirements11Unlessotherwisespecified(see...and23.3.6.5)allcontainertypesdefinedinthisClausemeetthefollowingadditionalrequirements:ifanexceptionisthrownbyaninsert()oremplace()functionwh

c++ - 是否允许将 std::vector 的元素插入到同一 vector 中?

考虑以下insert和emplacestd::vector的成员函数:templateiteratoremplace(const_iteratorposition,Args&&...args);iteratorinsert(const_iteratorposition,constT&x);iteratorinsert(const_iteratorposition,T&&x);iteratorinsert(const_iteratorposition,size_typen,constT&x);如果其中一个被引用vector本身的元素作为参数调用怎么办?通常,它们中的每一个都会使对从pos

C++11 vector push_back 不明确

考虑以下代码:#includestructS{inta;doubleb;};intmain(){std::vectorv;v.push_back({3,4.5});}g++4.4提示对push_back()的调用不明确:error:callofoverloaded‘push_back()’isambiguousnote:candidatesare:voidstd::vector::push_back(const_Tp&)[with_Tp=S,_Alloc=std::allocator]note:voidstd::vector::push_back(_Tp&&)[with_Tp=S,_A

c++ - 使用迭代器的 std::vector 模板构造函数是否允许转换?

在C++11标准第23.3.6.2节[vector.cons]中,如下所述:templatevector(InputIteratorfirst,InputIteratorlast,constAllocator&=Allocator());9Effects:Constructsavectorequaltotherange[first,last),usingthespecifiedallocator.10Complexity:MakesonlyNcallstothecopyconstructorofT(whereNisthedistancebetweenfirstandlast)andno

c++ - 是否可以指定一个空的 std::vector 作为默认参数?

我一直在阅读thissimilarquestion并且发现自己对这些react不太满意。我没有一个名为“foo”的简单函数;我有一个更复杂的函数,如果没有明智地提供参数,它就会非常危险。对我来说,执行自己的规则并确保系统稳健性的功能似乎比倾向于坚持“一个功能,一件事要做”的语义细节更可取。我认为对该问题的受欢迎回答反射(reflect)了编程理想主义走得太远,而不是某些情况的现实。我面对的是:voidDoSomethingVeryComplex(constCRectbounds,CPointstartPoint=CPoint(-1,-1));现在这恰好满足语义规则-它正在做某件事,而不

c++ - vector<T>::clear 可以抛出吗?

有没有机会调用std::vector::clear()抛出异常? 最佳答案 没有。[2003:21.2.1/11|n3290:21.2.1/10]:Unlessotherwisespecified(see23.2.4.1,23.2.5.1,23.3.3.4,and23.3.6.5)allcontainertypesdefinedinthisClausemeetthefollowingadditionalrequirements:[..]—noerase(),clear(),pop_back()orpop_front()functio

c++ - 通过 dll 边界传递对 STL vector 的引用

我有一个很好的库来管理需要返回特定字符串列表的文件。由于我将使用它的唯一代码将是C++(和Java,但这是通过JNI使用C++),我决定使用标准库中的vector。库函数看起来有点像这样(其中FILE_MANAGER_EXPORT是平台定义的导出要求):extern"C"FILE_MANAGER_EXPORTvoidget_all_files(vector&files){files.clear();for(vector::iteratori=file_structs.begin();i!=file_structs.end();++i){files.push_back(i->full_p

c++ - 我可以制作一个线程安全的 std::atomic<vector<int>> 吗?

我有一个需要执行的函数n=1000次。此函数执行蒙特卡罗风格模拟并返回int作为结果。我想运行nthreads=4在平行下。每当一个线程完成一个循环时,它应该将结果放在std::vector中。.因此,经过1000个循环后,我的vector为1000int可以通过统计来检验。自从std::vector不是线程安全的,我想到了std::mutex(这肯定会奏效)。但我想知道我是否可以将vector声明为原子的,从而绕过互斥锁?是否有可能拥有std::atomic>?我可以使用push_back等等? 最佳答案 C++11§29.5/1

c++ - 将 std::map 复制到成对的 std::vector 中

我正在尝试将map复制到一对vector中,因此我可以按second对vector进行排序对的数据成员。我已经解决了这个问题:voidmappedWordsListSorter(){for(autoitr=mappedWordsList.begin();itr!=mappedWordsList.end();++itr){vectorWordsList.push_back(*itr);}sort(vectorWordsList.begin(),vectorWordsList.end(),[=](pair&a,pair&b){returna.second>b.second;});}我需要找

c++ - 用另一个和额外元素的内容初始化 C++ (11) std::vector

我需要一种“优雅”的方式来在声明阶段使用另一个vector的内容和一些额外元素来初始化vector。我要解决的是:让我们考虑以下带有初始化的(示例)声明:conststd::vectorc90_types={"char","signedchar","unsignedchar","short","unsignedshort","int","unsignedint","long","unsignedlong","float","double","longdouble"};conststd::vectorc99_types={"char","signedchar","unsignedchar