草庐IT

C++迭代器(iterator)

全部标签

c++ - 为什么 istream_iterator<string>(ifstream ("test.txt")) 会导致错误?

我尝试编写代码从名为“test.txt”的文件中读取字符串并将字符串写入标准输出。下面的代码运行良好:intmain(){usingnamespacestd;ifstreamfile("test.txt");copy(istream_iterator(file),istream_iterator(),ostream_iterator(cout,""));}但是,通过此修改,代码不再编译:intmain(){usingnamespacestd;copy(istream_iterator(ifstream("test.txt")),//(),ostream_iterator(cout,""

c++ - STL 允许使用指向不同 map 的迭代器删除 map 的键/值吗?

所以,我偶然发现的是:std::mapmap1;std::mapmap2;map1[2.5]=11;map1[3.5]=12;map2[2.5]=21;map2[3.5]=22;std::map::iteratoriterMap1=map1.find(2.5);//Iwillnowtrytoeraseakey/valuepairinmap2withaniterator//thatpointstomap1.Thisisbad/wrong.ButIamsurprised//thisisallowed.map2.erase(iterMap1);//whatdoyouthinkwouldbep

c++ - 为什么将十进制转换为二进制的递归方法比迭代、使用和返回字符串的方法更快?

我创建了两个接受十进制数字并返回该数字的二进制表示的函数。我选择了一种简单的方法来执行此操作,即在进行一些简单的数学运算后将1和0连接到一个字符串。我创建了一个迭代递归方法来执行此操作。然后我用老师给我的计时器课对这两种方法进行了计时。事实证明,与我的迭代方法相比,我的递归方法大约快两倍。为什么会这样?stringCConversion::decimalToBinaryIterative(intnum){stringss;while(num>0){if(num%2!=0){ss='1'+ss;}else{ss='0'+ss;}num=num/2;}returnss;}stringCCo

C++:使用 clang 编译以迭代器作为值的映射时出现巨大错误

我正在尝试将迭代器用作std::map中的值,以便我可以通过对象的id高效地查找对象或通过其有效地迭代对象深度。考虑以下代码:#include#include#include#include#include#include#includestructobject{staticintnext_id;intid;intdepth;std::map::iteratorid_it;std::multimap::iterator>::iteratordepth_it;staticstd::mapby_id;staticstd::multimap::iterator>by_depth;object

c++ - 在 C++ 中迭代结构

我在迭代结构时遇到了一些问题。根据编译器标志,结构可以有不同的定义。我想将所有结构成员设置为0。不知道有多少个成员,但保证都是数字(int,long...)请看下面的例子:#ifdefFLAG1structstr{inti1;longl1;doulbed1;};#elsifdefined(OPTION2)structstr{doubled1longl1;};#elsestructstr{inti1;};#endif我想我想做的一个很好的伪代码是:voidf(str*toZero){foreachmembermintoZerom=0}有没有办法在C++中轻松做到这一点?

c++ - (void)r++ 输入迭代器的要求

在24.2.3Inputiterators下,C++标准将输入迭代器的要求之一指定为表达式(void)r++等效于(void)++r.您也可以在cppreference看到这个.这是什么表情?这个要求有什么意义?为什么需要它?它看起来像一个C风格的强制转换,以取消r++或++r的结果,但我认为这不是它的真实面目。也就是说,稍微有点题外话,我似乎可以在类中定义一个void转换运算符。gcc和clang都编译它,但clang给出了警告:warning:conversionfunctionconverting'C'to'void'willneverbeused 最

C++ : double iteration through map

我想遍历一个map,但内部循环只会遍历元素的上半部分。使用vector它看起来像这样:for(autoelement1=myVector.begin();element1!=myVector.end();++element1){for(autoelement2=element1+1;element2!=myVector.end();++element2){//mystuff}}但是对于mapelement1+1返回错误nooperatormatchesthisoperand..我相信这是因为元素在map中没有排序.那么我怎样才能正确地做到这一点呢?我目前正在使用这种需要在每个循环中进行

C++:偏移到 std::vector 迭代器的正确转换是什么?

我有一个函数,它接受double的std::vector,并将它们复制到另一个vector,但在特定的偏移量处(假设有足够的空间):voidcopy_stuff(conststd::vector&data,std::vector&dest,size_tdest_offset){std::copy(data.begin(),data.end(),dest.begin()+dest_offset);}这导致C++11clang编译器-Weverything警告集中在+dest_offset部分:Implicitconversionchangessignedness:'size_t'(aka

c++ - 将迭代器取消引用到临时范围时出现非指针操作数错误

使用autoempty_line=[](auto&str){returnstr.size()==0;};我们可以这样做:autoline_range_with_first_non_empty=ranges::view::drop_while(ranges::getlines(std::cin),empty_line);autoinput1=std::stoi(*line_range_with_first_non_empty.begin());我们也可以这样做:autoline_range2=ranges::getlines(std::cin);autoiter2=ranges::fin

c++ - 使用带有 split_iterator 的分类器

关于boost字符串算法,我似乎遗漏了一些东西。我正在尝试将split_iterator与使用Clasifier作为拆分点一起使用。所以,例如,我希望能够做这样的事情:make_split_iterator(str,is_space);但是make_split_iterator需要一个Range和一个Finder。所以我需要的是找到一些序列来从分类器创建查找器。有谁知道如何做到这一点,或者甚至可能吗? 最佳答案 您可以使用token_finder,如make_split_iterator(str,token_finder(is_sp