草庐IT

std-ranges

全部标签

c++ - 以高效的方式将 C 字符串转换为 std::vector<byte>

我想将C风格的字符串转换为字节vector。一个可行的解决方案是手动转换每个字符并将其推送到vector上。但是,我对这个解决方案并不满意,想找到更优雅的方式。我的尝试之一如下:std::vectormyVector;&myVector[0]=(byte)"MyString";哪个错误让我得到一个errorC2106:'=':leftoperandmustbel-value正确的做法是什么? 最佳答案 最基本的事情是这样的:constchar*cstr="bla"std::vectorvec(cstr,cstr+strlen(cst

c++ - std::ifstream 类的设计

我们这些看到了STL之美的人会尽可能多地使用它,并鼓励其他人在我们看到他们使用原始指针和数组的任何地方使用它em>。ScottMeyers写了一本关于STL的书,书名为EffectiveSTL.然而ifstream的开发人员发生了什么,他们更喜欢char*而不是std::string。我想知道为什么ifstream::open()的第一个参数是constchar*类型,而不是conststd::string&。请看一下它的签名:voidopen(constchar*filename,ios_base::openmodemode=ios_base::in);这是为什么?为什么不是这个:v

c++ - 为什么 std::search 需要转发迭代器

我的问题与下面的线程相同,我很难理解给出的答案,或者更确切地说,我的代码不应该工作,因为它只使用输入迭代器..但我的func似乎工作并且行为与std相同::search..所以我很茫然,不愿意在没有正确理解的情况下继续前进......也许如果有人可以提出一个会破坏我的功能但不会破坏std::的输入来自WhydoIneedaForwardIteratortoimplementmycustomizedstd::search:Iamstudyingthebook"AcceleratedC++"fromKoenig&Moo.Exercise8-2askmetoimplementonmyowns

c++ - std::string.npos 有效性

std::string.npos曾经有效吗?(与正确的std::string::npos相反。)我在我正在处理的一个旧项目中经常看到它,它不能用VS2010编译。它是前标准时代的东西吗? 最佳答案 用于命名类成员的带有类语法的C实际上是一个点:classX{public:voidf();};voidX.f()//adot!seeD&E2.3{}然而,::语法还没有被发明出来。std命名空间也不存在。因此,std::string.npos从未像Cwithclasses或标准C++那样有效。我怀疑std::string.npos纯粹是微

c++ - std::map of boost::mutex 具有奇怪的行为

我有这个代码://////DefaultNamespaces///usingnamespacestd;typedefmapt_map_shared_mutex;intmain(intargc,char**argv){t_map_shared_mutexlist_lock;boost::shared_mutexglobal_lock;stringi="ABC";boost::unique_lockl_lock(global_lock);boost::unique_locklock(list_lock[i]);//DoSomethingwiththatlocklock.unlock();l

c++ - std::priority_queue<> 什么时候进行 self 排序?

我想知道什么时候C++STLpriority_queue自行排序。我的意思是它insert当你push中的项目,或者当你peek时,它会自行排序并给你最高优先级的项目吗?或pop出来?我问这个是因为我的priority_queue将包含一个可能有值更新的数组的索引,我希望它在我执行pq.top();时更新.#include#include#includeusingnamespacestd;intmain(){priority_queuepq;pq.push(2);pq.push(5);//isthefirstelement5now?orwillitupdateagainwhenItop

c++ - std::map.insert "could not deduce template argument for..."

我正在尝试熟悉STL库,但我无法理解我的编译错误。我使用编译器错误字符串“无法推断...的模板参数”搜索了其他问题,但没有一个答案似乎适用或相关。Error4errorC2784:'boolstd::operator&,conststd::unique_ptr&)':couldnotdeducetemplateargumentfor'conststd::unique_ptr&'from'conststd::string'c:\programfiles(x86)\microsoftvisualstudio10.0\vc\include\xfunctional125我正在编写一个简单的解释

c++ - 为什么 std::getline 不阻塞?

我在Objective-C类中有这段代码(在Objective-C++文件中):+(NSString*)readString{stringres;std::getline(cin,res);return[NSStringstringWithCString:res.c_str()encoding:NSASCIIStringEncoding];}当我运行它时,我每次都会得到一个零长度的字符串。从来没有机会在命令行键入。没有什么。当我将这段代码逐字复制到main()中时,它起作用了。我在build设置下打开了ARC。我不知道发生了什么。OSX10.7.4,Xcode4.3.2。它是一个控制台

c++ - 在赋值运算符中分配 std::shared_ptr

我正在创建自己的自定义Filter类以用于boost::filtered_graph。WeightMap概念必须具有默认构造函数、复制构造函数和赋值运算符。我创建了下面的类,它有一个std::shared_ptr私有(private)成员。我的问题是我应该如何编写赋值运算符。复制构造函数没有问题,但赋值运算符不起作用。classBFDMFilter{private:constBGraph*m_battlemap;conststd::shared_ptrm_mv_ab;public:BFDMFilter():m_battlemap(nullptr),m_mv_ab(){}BFDMFilt

c++ - is_lock_free 未在 gcc 4.7.2 的 std::atomic<T> 中定义?

我遇到这个编译器错误functionstd::atomic::is_lock_free()const:error:undefinedreferenceto'__atomic_is_lock_free'whencompilingcodelikebelowusinggcc4.7.2onlinux.structS{inta;intb;};std::atomics;cout 最佳答案 AtomicAPIisn'tcompleteinGCC4.7:Whenlockfreeinstructionsarenotavailable(eitherth