草庐IT

ostream_iterator

全部标签

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++ - 将字符串 vector 连接到 std::ostream(如 boost::join)

我有一个字符串vector,我想将它输出到流(实际上是文件流)。我想在vector元素之间有一个分隔符。有一种方法可以使用标准ostream_iteratorstd::vectorstrs;std::ostream_iteratorout_file_iterator(out_file,delim);std::copy(strs.begin(),strs.end(),out_file_iterator);我不喜欢这种方式,因为each元素后有一个delim文本,但我不需要有一个delim在最后一个元素之后。我想使用类似boost::join的东西。但是boost::join返回字符串,而

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++ - ostream::write 实际写入了多少字节?

假设我向ostream::write发送了一个大缓冲区,但实际上只有它的开始部分成功写入,其余部分没有写入intmain(){std::vectorbuf(64*1000*1000,'a');//64mbytesofdatastd::ofstreamfile("out.txt");file.write(&buf[0],buf.size());//trytowrite64mbytesif(file.bad()){//butsupposeonly10megabytewereavailableondisk//howmanywereactuallywrittentofile???}return

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++ - 检查给定的 ostream 对象是否已写入

我可以查询ostream对象是否已写入吗?对于ostringstream,可以使用if(!myOssObject.str().empty())一般情况如何,例如ofstream或cout或cerr? 最佳答案 一般没有。您可以通过tellp()查看在刷新(发送缓冲数据)之前写入了多少字符(或其他内容):Returnstheoutputpositionindicatorofthecurrentassociatedstreambufobject.cout0){//Thereissomedatawritten}刷新后,这些输出流将忘记它们

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::ostream 需要功能方面的帮助

我需要有人逐部分向我解释这些代码行,我需要一些帮助来使用简单示例的“ostream”。谢谢:)。inlinestd::ostream&operator更新1:当我使用此函数时,它无法编译并且错误提示:std::ostream&class::operator 最佳答案 这些行只是将处理Telegram对象的能力添加到标准输出流类。当你添加一个新类并且你想要像cout这样的输出流时要智能地处理它们,您需要添加一个新的将新对象类型作为第二个参数的运算符方法。上面的代码就是这么做的。当您稍后执行语句时:Telegramtg("Bob","H

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

c++ - istream_iterator cin 初始化等待输入

我有这个代码片段。istream_iterator对象仅被定义而未被使用,因此我预计它不会执行任何操作并且应用程序会立即完成。但是当我运行应用程序时,在我提供一些输入之前它不会完成。为什么?我在ArchLinux上编译它:gcc4.7.1,命令:g++-std=c++11filename.cpp#include#includeusingnamespacestd;intmain(intargc,char*argv[]){istream_iteratorinput(cin);return0;} 最佳答案 按照标准,24.6.1.1ist