草庐IT

c++ - C++ 代码 `x.erase(std::remove(x.begin(), x.end(), ' '), x.end())` 是如何工作的?

问题是我通常会使用for循环来处理这种事情,但这种方法似乎更有效率。cplusplus的文档对我来说有点难以理解。std::stringno_space(std::stringx){x.erase(std::remove(x.begin(),x.end(),''),x.end());returnx;} 最佳答案 函数std::remove(x.begin,x.end),'')将元素移动到字符串的末尾,函数std::erase实际上删除了被移动到字符串末尾的元素。您还可以在文档中阅读更多相关信息enterlinkdescription

c++ - 右值引用和 SFINAE

我最近开始使用右值引用,但我遇到了一个案例,我不明白为什么它们会这样工作。我正在尝试确定一个类型是否可以调用begin和end。如果我更改foo以按值或const引用获取它的参数,下面的代码会给出预期的结果,但我不确定为什么在使用右值引用时它不起作用,我想知道是否谁能告诉我为什么。#include#include#includetemplateautobegin(Container&&c)->decltype(c.begin()){returnc.begin();}templateautoend(Container&&c)->decltype(c.end()){returnc.end(

c++ - begin() 如何知道要返回哪种返回类型(const 或 non-const)?

这非常有效:listl;list::const_iteratorit;it=l.begin();list::iteratorit2;it2=l.begin();我不明白list是怎么来的“知道”它必须返回iteratorbegin()版本或const_iteratorbegin()const一个。我正在尝试为我的容器(一个trie)实现迭代器,但我遇到了这个问题。难道C++不应该不按返回类型处理差异化(除非使用奇怪的技巧)?这是一些代码和我得到的编译器错误:我的Trie是一个模板化的trie,可以包含任何类型。我有一个Trie::iter非常量迭代器和一个Trie::const_ite

c++ - 匹配可迭代类型(具有 begin()/end() 的数组和类)

这个问题在这里已经有了答案:Checkifavariabletypeisiterable?(6个答案)关闭9个月前。我写了类型特征,比如可以用来测试给定类型是否“可迭代”的类。对于数组(对于T[N],而不是对于T[])和具有begin和的类来说都是如此>end方法返回看起来像迭代器的东西。我想知道是否可以做得比我做的更简洁/更简单?特别是impl命名空间中的东西看起来有点迂回/hacky。这一切在我看来都有点难看。有关使用它并可以用g++和clang++编译的示例,请参见:https://gist.github.com/panzi/869728c9879dcd4fffa8templat

C++ 从 char* 到 char 的无效转换 (char* = *string.begin() )

我有以下代码:std::stringextract(){fstreamopenfile("/home/name/Documents/testfile");std::stringteststring;longlocation=4;longlength=2;teststring.resize(length);char*begin=*teststring.begin();openfile.seekp(location);openfile.read(begin,length);returnteststring;}此代码应该返回在文件中找到的字符串。例如,如果文件的内容是StackOverflo

c++ - 命名空间 'begin' 中没有名为 'std' 的成员

我在Windows上成功编译了一个应该是跨平台的代码。现在,当使用MacOSX在Xcode中编译它时,我得到:std::valarrayv(32);...std::sort(begin(v),end(v));#Useofundeclaredidentifier'begin'std::sort(std::begin(v),std::end(v));#Nomembernamed'begin'innamespace'std'std::sort(std::valarray::begin(v),std::valarray::end(v));#Idem,erroraswell为什么会发生错误Nom

c++ - 为具有 "end"成员变量的类型启用基于范围的 for

我正在使用avectortype来自C库,看起来类似于structVec{int*stor_begin;int*stor_end;int*end;};我试图通过创建免费的begin()和end()函数为这种类型启用基于范围的for循环,但是我从clang得到了这个错误:error:rangetype'igraph_vector_int_t'has'end'memberbutno'begin'member有没有办法(使用C++11)为这种类型(我不能直接修改)启用基于范围的for循环?这是一个演示问题的最小示例://NoproblemswithFoostructFoo{int*fooBe

c++ - 检查迭代器是否在另一个迭代器之前

我想检查迭代器(或类似类型)是否在另一个迭代器之前。在这个例子中,我想检查我没有无限循环。这意味着,“如果我在开始时应用运算符++的次数足够多,我就会结束”。在C++98中可能吗?也许对类型T有限制?/***\briefLoop*\attentionTmustimplementoperator++()andoperator!=(constT&)*\parambeginBeginoftheloop*\paramendEndoftheloop*\prebeginprecedesend*/templatevoidloop(constT&begin,constT&end){Trun=begin

c++ - std::begin 如何推导 braced-init-list 的类型?

以下代码失败:templatevoidfunc(T&t){}intmain(){func({1,2,3});}但是对于autoa={1,2,3};它是有效的,因为规则允许auto推导出一个std::initializer_list。std::begin如何编写以允许std::begin({1,2,3})工作? 最佳答案 std::begin({1,2,3})有效是因为std::begin有一个overloadtakinganstd::initializer_list. 关于c++-std

c++ - const decltype(*std::begin(container))& val 不会使 val const?

这段代码:std::vectorints(5,1);std::for_each(ints.begin(),ints.end(),[](constdecltype(*std::begin(ints))&val){val*=2;});在VisualStudio2010中编译和运行得很好,并且修改容器中的每个值,就像没有const关键字一样。这是编译器中的错误吗,因为预期的行为是val是不可修改的?(换句话说,我希望它不会编译,但它会编译)更新:std::for_each(ints.begin(),ints.end(),[](conststd::remove_reference::type&