草庐IT

iter_swap

全部标签

C++ : syntax for passing iterator to a function?

我正在制作一个类,它是一种容器,我想制作一个可以采用“第一个”和“最后一个”迭代器的构造函数,如std::vector和其他标准容器。正确的语法是什么?(我想要一个模板函数,它可以接受任何可用的第一个/最后一个迭代器类型(就像我认为的标准库)。非常感谢!举个例子,我想要这样的东西:templateMyClass(...first,...last)但是……是什么?非常感谢。关于第一个答案:我想要一个将迭代器作为参数的特定构造函数(因为我已经有了将值和指针作为参数的构造函数)编辑:这样可以吗?templateMyClass(std::iteratorfirst,std::iteratorl

c++ - 是否可以推断出 std::insert_iterator 包含的类型?

我有一个需要模板化迭代器类型的函数。它当前取消引用迭代器以检查被迭代的类型。templatevoidfunc(Iteratori){//Inspectthesizeoftheobjectsbeingiteratedconstsize_ttype_size=sizeof(*i);...}我最近发现一些标准迭代器类型,例如std::insert_iterator将*i定义为对i的简单引用.即sizeof(*i)是迭代器本身的大小;与sizeof(i)或sizeof(***i)相同是否有一种通用方法(支持C++03)来确定任何标准迭代器正在迭代的对象的大小或类型?

c++ - 为什么 std::copy 或 std::swap 不需要 <algorithm>?

根据这个cplusplus.com页,std::copy在header,原样std::swap然而这有效:#include//std::cout#include//std::vector#include//std::ostream_iterator()#include//rand(),srand()//NOTincludingintmain(){srand(time(NULL));constintSIZE=10;std::vectorvec;for(inti=0;i(std::cout,""));std::cout我唯一能想到的是它们是由导出的也...但是为什么我们需要标题吗?

c++ - 如何为非 const 类调用 const_iterator?

这个问题在这里已经有了答案:Howtousetwofunctions,onereturningiterator,theotherreturningconst_iterator(2个答案)关闭8年前。我阅读了一些与此问题相关的其他线程,但没有为我的问题提供解决方案。希望大家给我出出主意或建议。我正在尝试实现这个名为Map的类。它应该包含2个迭代器-iterator和const_iterator。我实现了它们-iterator继承自const_iterator,在Map类中我有以下函数:iteratorbegin();iteratorend();const_iteratorbegin()c

C++ 编译器无法识别 std::stringstream::swap

我正在尝试使用g++(GCC)4.8.220131212(RedHat4.8.2-7)编译以下代码:#includeusingnamespacestd;intmain(intargc,char**argv){autox=1;stringstreams1,s2;s1.swap(s2);}我收到以下错误:g++-g-std=c++0x-cmain.cppmain.cpp:Infunction‘intmain(int,char**)’:main.cpp:8:5:error:‘std::stringstream’hasnomembernamed‘swap’s1.swap(s2);^make:*

c++ - 当没有任何意义时, `Iterator::pointer` 使用什么?

例如,考虑一些假设的to_upper_iterator遍历一系列字符,返回std::toupper对于operator*.这些迭代器别名对我来说很有意义:templatestructto_upper_iterator{usingvalue_type=CharT;usingreference=CharT;usingdifference_type=std::ptrdiff_t;usingiterator_category=std::random_access_iterator_tag;};没有意义的是什么应该/可以用于pointer别名。我试着把它关掉,但果然我遇到了编译错误。似乎这是由于

c++ - std::map::const_iterator 泄露了对值的非常量引用?

我观察到std::map::const_iterator泄漏了对value_type的非常量引用:#include#includeintmain(intargc,char*argv[]){std::mapfoo={{1,1},{4,2}};constauto&m=foo;constauto&it=foo.find(1);printf("%d%d\n",it->first,it->second);int&i=it->second;i=3;auto&one=foo.at(1);printf("%d%d\n",1,one);return0;}输出$g++test.cc&&./a.out111

c++ - 如果容器元素是指针,为什么允许我从 const_iterator 调用非 const 成员函数?

考虑以下代码:#includeusingnamespacestd;structfoo{voidbar(){}};intmain(){{vectora;a.push_back(newfoo());a.push_back(newfoo());a.push_back(newfoo());vector::const_iteratoritr=a.begin();(*itr)->bar();//compiles-thisbecomesmoreconfusing//whenfoundinaconstmethod.Onfirst//glance,onewill(oratleastme)may//ass

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++ - 为什么内存泄漏只发生在赋值运算符重载的情况下而不发生在复制构造函数中以及 copy-and-swap 习语如何解决它

P.S:我是编程新手,所以请用更简单的术语回答我的疑问。我找到了几个答案,但无法理解。下面是复制构造函数和赋值运算符重载。templateMystack::Mystack(constMystack&source)//copyconstructor{input=newT[source.capacity];top=source.top;capacity=source.capacity;for(inti=0;iMystack&Mystack::operator=(constMystack&source)//assignmentoperatoroverload{input=newT[sourc