草庐IT

std-variant

全部标签

c++ - 为什么我不能将此对象推送到我的 std::list 中?

刚开始用C++编程。我创建了一个Point类、一个std::list和一个迭代器,如下所示:classPoint{public:intx,y;Point(intx1,inty1){x=x1;y=y1;}};std::listpointList;std::list::iteratoriter;然后我将新点推送到pointList。现在,我需要遍历pointList中的所有点,所以我需要使用迭代器进行循环。这就是我搞砸的地方。for(iter=pointList.begin();iter!=pointList.end();iter++){PointcurrentPoint=*iter;gl

C++ 自动类型转换为 std::string 和 char* 的区别

作为学习练习,我一直在研究C++中的自动类型转换是如何工作的。我知道通常应该避免自动类型转换,但我还是想通过了解它的工作原理来增加我对C++的了解。我已经创建了一个可以自动转换为std::string的StdStringConverter类,但是编译器(Debian上的g++4.3.4)似乎没有这样做将对象与真实的std::string进行比较时的转换(请忽略缺少按引用传递和不必要地创建临时对象的情况):#includeclassStdStringConverter{public:explicitStdStringConverter(std::stringname):m_name(na

C++,重载 std::swap,编译器错误,VS 2010

我想在我的模板类中重载std::swap。在下面的代码中(简化)#ifndefPoint2D_H#definePoint2D_HtemplateclassPoint2D{protected:Tx;Ty;public:Point2D():x(0),y(0){}Point2D(constT&x_,constT&y_):x(x_),y(y_){}....public:voidswap(Point2D&p);};templateinlinevoidswap(Point2D&p1,Point2D&p2){p1.swap(p2);}namespacestd{templateinlinevoidsw

c++ - std::vector 的 Typedef 和 ostream 运算符

我创建了一个Chromosome类,它最终只是一个带有ostream运算符的vector包装器,所以我决定改用typedefvector。但是,我在使用模板化的ostream运算符时遇到了问题……这是最好的方法吗?(我见过一些方法,但都没有奏效)templateclassChromosome{public:typedeftypenamestd::vectortype;typedeftypenamestd::pairptr_pair;};template//line19below:std::ostream&operator::type&chromosome){for(autoiter=c

c++ - 复制 std::ofstream 追加内容

我正在使用std::ofstream进行跟踪输出。出于某些原因,有时我想将附加在std::ofstream末尾(尚未刷新或关闭)的内容复制到另一个std::ofstream中;您有什么办法可以实现吗?谢谢 最佳答案 Tee从Boost.Iostreams过滤可以将输出流分成两部分。这是一个深受JohannesSchaub给出的启发的例子在他的回答中here.#include#include#include#includeintmain(){namespaceio=boost::iostreams;typedefio::tee_dev

c++ - solaris (x86) 上std::basic_string 的一些疑惑

solaris(x86)上std::basic_string的一些困惑#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws(s,s+6);for(inti=0;i运行结果为:9799101000为什么不是979899100101102代码#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws;ws.resize(6);for(inti=0;i可以得到预期的结果。我使用gcc3.4.6,构建命令是g++-fshort-wcharstri

c++ - std::bad_alloc 之后 std::vector 的状态

我试图找到一个在线引用来查看几个标准容器的异常安全性。在std::vector的情况下,它是否保持push_back调用之前的状态?我假设vector的所有对象仍然有效(没有调用析构函数)。在push_back抛出std::bad_alloc异常后,提供什么保证std::vector? 最佳答案 如果它抛出,vector不会改变。甚至不是capacity()。根据[container.requirements.general]:Unlessotherwisespecified(see23.2.4.1,23.2.5.1,23.3.3.

c++ - std::call_once 和内存重新排序

给定来自here的代码:classlazy_init{mutablestd::once_flagflag;mutablestd::unique_ptrdata;voiddo_init()const{data.reset(newexpensive_data);}public:expensive_dataconst&get_data()const{std::call_once(flag,&lazy_init::do_init,this);return*data;}};我在其他地方也看到了相同模式的一些变体。所以我的问题是:为什么这段代码被认为是保存的?以及为什么编译器不能在调用std::c

c++ - std::unique_ptr<T> 不完整类型错误

我有templateclassqueue{private:structnode{Tdata;std::unique_ptrnext;//compileerroronincompletetypenode(T&&data_):data(std::move(data_)){}};std::unique_ptrhead;node*tail;public:queue():tail(nullptr){}我在VS10的标记行上遇到编译错误。在这种情况下,我是否应该被允许使用不完整的类型(实例化模板-构造函数-这里以int为例)?有解决方法吗?编辑singlethreadedqueue.h(62):e

c++ - 命名空间 std 中所有符号的列表

关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭6年前。Improvethisquestion我在哪里可以找到在命名空间std中声明的所有符号的列表?实在找不到,不知道去哪里找。