草庐IT

STD_PROP_LIST

全部标签

c++ - std::thread 管理:使用和最佳实践

在Java中使用了一些线程后,我试图找出线程,但我有点困惑。两个问题:我可以从线程扩展我的类,还是必须通过处理程序从类内部管理线程?如何保存所述线程处理程序?std::thread本身似乎没有命名类型。任何方向正确的产品将不胜感激。我如何解读这条消息?src/CHandler.h:27:9:error:'thread'innamespace'std'doesnotnameatypestd::thread_thread;^这是我扩展线程的尝试:src/CHandler.h:17:30:error:expectedclass-namebefore'{'tokenclassCHandler:

c++ - getline 和 std::istream::operator>>() 有什么区别?

#include#includeusingnamespacestd;intmain(){stringusername;cout>username;}所以我很好奇这两个代码之间有什么区别,我听说这是同一件事,但如果是,那为什么要用两种方法呢?#include#includeusingnamespacestd;intmain(){stringusername;cout 最佳答案 区别在于std::getline—顾名思义—从给定的输入流(可能是std::cin)和operator>>中读取行读一个单词1。即std::getline读取直

c++ - 在 C 中使用 printf 等效于 C++ std::setprecision(20)

我想以十进制表示法完全精确地打印double(但数字末尾没有额外的零)。在C++中,我可以使用:std::setprecision(20);cout使用printf的等效C代码是什么? 最佳答案 您可以使用“%.20g”说明符。g恕我直言,通常比f好,因为它不打印尾随零,并且明智地处理大/小值(更改为e格式)。另请注意,使用“g”说明符时,精度(在本例中为“20”)指定有效位数而不是小数点后的位数。 关于c++-在C中使用printf等效于C++std::setprecision(20)

c++ - 修剪 std::string 中的内部空格

我正在寻找一种优雅的方式来转换std::string,例如:std::stringtext="a\tvery\tugly\t\t\t\tstring";收件人:std::stringtext="averyuglystring";我已经用boost::trim(text);修剪了外部空白[编辑]因此,多个空格和制表符被缩减为一个空格[/编辑]删除外部空白是微不足道的。但是有没有一种优雅的方法可以删除内部空白,而不涉及手动迭代和比较前后字符?也许我错过了boost中的某些内容? 最佳答案 您可以使用std::unique和std::re

c++ - 如何从 std 字符串 (c_str()) 设置 char * 值不起作用

我不知道,但这对我不起作用,当我尝试从返回标准字符串的函数中设置char*值时,我得到了垃圾值:stringfoo(){stringtmp="dummyvalue";returntmp;}char*cc=(char*)foo().c_str();//ifiremovethecastingimgettingerror//wheniprinttheccigetgarbageprintf("%s",cc); 最佳答案 cc指向的数据的生命周期与它来自的字符串的生命周期相同(充其量-如果您修改字符串,它甚至更短)。在你的例子中,foo()的

c++ - 如何有效地将 std::vector<char> 复制到 std::string

这个问题是这个Howtoefficientlycopyastd::stringintoavector的反面我通常以这种方式复制vector(空终止字符串)std::strings((char*)&v[0]);或者(如果字符串已经被声明)像这样s=(char*)&v[0];它完成了工作,但也许还有更好的方法。编辑C风格的转换很丑陋,有人告诉我这又如何s=reinterpret_cast(&vo[0]); 最佳答案 只需使用迭代器构造函数:std::strings(v.begin(),v.end());(编辑):或者使用char-poi

c++ - 尝试访问 std::stack 的索引

voidPDA::parse(vectorwords){for(inti=0;i我遇到了这些错误PDA.cpp:25:error:nomatchforâoperator[]âinâ((PDA*)this)->PDA::stack[j]âPDA.cpp:26:error:nomatchforâoperator[]âinâ((PDA*)this)->PDA::stack[(j-1)]â对于这些行if(!stack[j]){//sincej-1whentheindexis0willcauseanerrorif(stack[j-1]==matchingBracket){我查找了std::sta

c++ - std::shuffle 不能用 std::list 编译

我正在尝试打乱一些生成元素的列表。这是代码:std::default_random_enginegenerator(10);std::listlist(10);intn=0;std::generate(list.begin(),list.end(),[&]{returnn++;});std::shuffle(list.begin(),list.end(),generator);它不编译。以下是错误:/include/c++/v1/algorithm:3059:34:Invalidoperandstobinaryexpression('std::__1::__list_iterator'

c++ - 是否保证 std::unique_ptr 删除顺序?

我正在制作一个控制我的应用程序的全局单例,我希望子系统以特定顺序启动和关闭。classApp{public:App();~App();voidstart();voidrun();voidshutdown();private:std::unique_ptrdisplayManager;std::unique_ptrrenderer;};构造函数以正确的顺序创建指针App::App(){displayManager=std::unique_ptr(newDisplayManager);renderer=std::unique_ptr(newRenderer);}并且我希望以相反的顺序释放u

C++ list<T>::iterator 不能在派生类模板中使用

g++编译器给出了这个错误:expected`;'在“它”之前templateclassmyList:publicstd::list{public:voidfoo(){std::list::iteratorit;//compilererrorasabovementioned,why???}};谢谢。 最佳答案 在g++中。每当在模板中看到错误时:error:expected';'before'it'怀疑你需要一个类型名:typenamestd::list::iteratorit;当您在模板中声明了一个依赖于一个或多个模板参数的新类型