jjzjj

cppReference

全部标签

c++ - 为什么我不能在列表迭代器上使用 += 运算符?

我有一个来自std::list的迭代器,但是当我尝试使用+=推进它时,我得到一个编译错误。代码是:#include#include#includeintmain(){std::listx;x.push_front("British");x.push_back("character");x.push_front("Codingisunco");x.push_back("Society");x.push_back("CityHole");autoiter=x.begin();iter+=3;//std::advance(iter,3);x.erase(iter);for(auto&e:x)

c++ - 来自 cppreference.com 的模板参数特化示例不起作用

我在http://en.cppreference.com/w/cpp/language/partial_specialization上找到了这个例子templatestructB{};templatestructB{};//OK:firstparameterisdeducible我在使用-std=c++11和-std=c++14编译时出错如何编译这个?或者也许例子是错误的?error:templateargument‘(I*2)’involvestemplateparameter(s)templatestructB{};//OK:firstparameterisdeducible

c++ - set::vector 初始化用数字引号

所以我想到了一个关于std::reduce的问题,这让我想到了一个例子。cppreference.com在这个例子中,我看到了以下std::vector声明:std::vectorv(10'000'007,0.5);这些引用在这里做什么?我以前从未在任何地方见过这个。这是我应该了解的一种新型C++功能吗? 最佳答案 这是C++14的新特性。来自cppreference:Optionalsinglequotes(')maybeinsertedbetweenthedigitsasaseparator.Theyareignoredbyth

c++ - 如何在 C++ 中动态存储和访问类型?

我知道C++模板,它允许您为多种类型编写代码,但如果我想动态存储和访问一个类型怎么办?为什么这在C++中很难做到?我非常希望不必必须做这样的事情:enumSupportedTypes{IntType,FloatType,StringType}templateclassClassThing{public:TValue;SupportedTypesType;}...//Notsureifyoucouldevenaccessthing->Type,butregardless,yougettheidea...switch(thing->Type){caseIntType:DoSomething

c++ - 有效类型的 memmove 就地更改(类型双关)

在下面的问题中:What'saproperwayoftype-punningafloattoanintandvice-versa?,结论是从整数位构造double的方法是通过memcpy构造double,反之亦然。很好,找到的pseudo_cast转换方法是:templateinlineTpseudo_cast(constU&x){static_assert(sizeof(T)==sizeof(U));Tto;std::memcpy(&to,&x,sizeof(T));returnto;}我会这样使用它:intmain(){static_assert(std::numeric_limi

c++ - 快速 C++ 字符串输出

我有一个程序可以从FPGA输出数据。由于数据变化非常快,我试图提高程序的速度。现在我正在打印这样的数据for(inti=0;i我发现使用一个printf大大提高了速度printf("data:%d\ndata:%d\ndata:%d\n",getData(1),getData(2),getData(3));但是,如您所见,它非常困惑,我无法使用for循环。我尝试先使用sprintf连接字符串,然后一次打印所有内容,但它和第一种方法一样慢。有什么建议吗?编辑:我已经先打印到一个文件,因为我意识到控制台滚动会是一个问题。但还是太慢了。我正在为外部FPGA调试内存Controller,因此越

c++ - std::strtol 和 std::stoi 之间有什么区别?

免责声明:链接指向cppreference.com所以我早就知道std::atoi已被弃用,建议使用std::strtol反而。C++11引入了std::stoi我试图理解为什么人们会选择使用它而不是std::strtol。据我了解,stoi调用strtol但抛出异常。它还返回一个整数而不是一个长整数。这些是主要区别吗,我错过了什么? 最佳答案 Arethesethemaindifferences,whatamImissing?较新的std::stoi也可以直接从std::string运行(因此您不必在代码中乱加.c_str()调用

c++ - 使用数组和某些方法来 memcpy struct 是否安全?

我想知道在包含数组和方法的结构上使用memcpy是否安全(只是一些getter和setter,因为数组的索引是不寻常的,我必须以某种方式映射它)。我知道它对POD是安全的,但我不确定我的结构是否会被视为POD? 最佳答案 您可以使用memcpy如果struct是TriviallyCopyable.您可以检查您的struct可以通过使用std::is_trivially_copyable轻松复制.此外,正如@JohanLundberg在评论中指出的那样,目标地址必须是0模std::alignment_of.您可以在http://en.

c++ - 可以在 constexpr 上下文中使用导致未指定(不是未定义!)行为的指针的表达式吗?

根据cppreference(强调我的):Acoreconstantexpressionisanyexpressionthatdoesnothaveanyoneofthefollowinginanysubexpression(...)Anexpressionwhoseevaluationleadstoanyformofcorelanguageundefinedbehavior(includingsignedintegeroverflow,divisionbyzero,pointerarithmeticoutsidearraybounds,etc).Whetherstandardlibr

c++ - 通知前完成手动解锁

多亏了ScottMeyers医生的书,第263页,我最近发现了condition_variable,所以我不得不搜索它oncppreference进一步研究。https://en.cppreference.com/w/cpp/thread/condition_variable我有几个问题,因为我想了好几天了,但我还是不明白。我的问题是关于这段代码://Manualunlockingisdonebeforenotifying,toavoidwakingup//thewaitingthreadonlytoblockagain(seenotify_onefordetails)lk.unloc
12