草庐IT

std-ranges

全部标签

c++: 'std::is_fundamental' 的替代方案?

在模板类中的函数中,我试图区分基本类型和其他类型。在C++11中你可以这样做:if(std::is_fundamental::value){//Treatitasaprimitive}else{//Treatitotherwise}如果我错了,请纠正我,这不仅在C++11中。在早期版本的c++中是否有替代方案? 最佳答案 你可以使用Boost'stypetraits在C++03中是这样的:#include...if(boost::is_fundamental::value){//Treatitasaprimitive}else{//

c++ - 向 std::vector 或 typedef 添加自定义方法

所以我有以下代码可以在vector中“搜索”对象的字符串。#include#include#include#include#includestructmigObj{migObj(conststd::string&a_name,conststd::string&a_location):name(a_name),location(a_location){}std::stringname;std::stringlocation;};intmain(){typedefstd::vectormigVec;migVecv;v.push_back(migObj("fred","belfast"));

c++ - std::reverse_copy "error: function call has aggregate value"

#include#include#include#includeusingnamespacestd;intmain(){intarrA[]={1,2,3,4,5,6,7,8,9};vectorvecIntA(arrA,arrA+sizeof(arrA)/sizeof(arrA[0]));vectorvecIntB(vecIntA.size());//copy((vecIntA.rbegin()+3).base(),(vecIntA.rbegin()+1).base(),vecIntB.begin());//OKvector::iterators=(vecIntA.rbegin()+3)

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++ - std::to_string 与字符串流

下面的代码显示了2个解决方案(std::to_string和std::stringstream)转换intm_currentSoundTime到std::string。std::to_string或std::stringstream更快吗?//ComputecurrentsoundtimeinminuteandconverttostringstringstreamcurrentTime;currentTime或m_currentSoundTimeInMinute=to_string(m_currentSoundTime/60); 最佳答案

c++ - 检查 C++11 中返回的 std::function 是否为 "valid"

我想像这样实现一个动态任务队列:typedefstd::functionJob;typedefstd::functionJobGenerator;//..JobGeneratorgen=...;autojob=gen();while(IsValidFunction(job)){job();}如何实现IsValidFunction?std::function是否有某种默认值可供检查? 最佳答案 您可以简单地检查job作为一个bool值:while(autojob=gen()){job();}这是一种简写形式,它赋值job来自gen()

c++ - 如何迭代 std::string 中的所有正则表达式匹配及其在 c++11 std::regex 中的起始位置?

我知道两种从std::string获取正则表达式匹配的方法,但我不知道如何获取所有匹配及其各自的偏移量。#include#include#includeintmain(){usingnamespacestd;strings="123apples456oranges789bananasorangesbananas";regexr=regex("[a-z]+");constsregex_token_iteratorend;//hereIknowhowtogetalloccurences//butdon'tknowhowtogetstartingoffsetofeachonefor(sreg

c++ - 将函数分配给 std::function 类型时调用哪个函数原型(prototype)(以及如何调用)?

我有密码voidprints_one(){coutfoo;foo=prints_one;foo();return0;}它按预期工作;它打印“一个”。我不知道在赋值中调用了哪个赋值运算符原型(prototype)以及如何调用。看cppreference,好像就是这个函数templatefunction&operator=(Fn&&fn);但如果这是被调用的原型(prototype),我不明白函数如何绑定(bind)到右值引用。谢谢!更新:谢谢大家,我会阅读通用引用资料。关于40two的回答;此代码打印它是一个右值引用:templateclassFoo{public:Foo(){}Foo&

c++ - 虚拟重载与 `std::function` 成员?

我现在有一个类,我们称之为Generic.这个类有成员和属性,我打算在std::vector中使用它或类似的,处理这个类的几个实例。另外,我想特化这个类,通用对象和特化对象之间的唯一区别是一个私有(private)方法,它不访问类的任何成员(但被其他方法调用)。我的第一个想法是简单地声明它virtual并像这样在专门的类中重载它:classGeneric{//allothermembersandattributesprivate:virtualfloatspecialFunc(floatx)const=0;};classSpecialized_one:publicGeneric{pri

c++ - OS X 上的 std::locale 段错误,无法在任何其他平台上重现

我有以下代码建议用于我之前的问题generaterangeforwhichstd::isalphaevaluatestotrue.它显示当前区域设置中的所有char,isalpha的计算结果为true,因此对于英语区域设置,它显示AB...Zab...z。但是,当我尝试访问table。我在OSXMavericks10.9.4上用g++4.9和clang++(AppleLLVMversion5.1(clang-503.0.40))编译了它。在所有其他平台(Linux/Solaris)上它都可以工作,没有段错误。谁能指出OSX上C++的libc++/libstdc++是否存在问题,或者至少