草庐IT

c++ - 为什么 stringstreams rdbuf() 和 str() 给我不同的输出?

我有这个代码,intmain(){std::stringst;std::stringstreamss;ssstr():"str();std::cout给我这个输出ss.rdbuf()->str():hejhejmeddigss.rdbuf():hejmeddig但这是为什么呢?是因为ostreams对operator 最佳答案 ss.rdbuf()->str();返回所有缓冲区内容的拷贝。在做什么std::cout?查看说明basic_ostream&operator*sb);它从缓冲区逐个字符地读取并将它们写入ostream,直到

c++ - std c++ 容器元素销毁和插入行为

我做了以下小程序:(基本上是一个检查它是否被创建、复制或销毁的类,以及一个执行其中一些操作的主类)classFoo{public:Foo(stringname):_name(name){coutv1,v2;system("PAUSE");v1.push_back(albert);system("PAUSE");v2.push_back(bert);system("PAUSE");v1=v2;system("PAUSE");}system("PAUSE");}输出看起来像这样:InstanceAlbertofclassFoocreated!InstanceBertofclassFoocr

c++ - std::ostream 第一次使用时未正确格式化 const char*

我一直在编写自定义std::streambuf作为日志系统的一部分。但是,我遇到了流输出的第一段格式不正确的问题。这是一个不使用任何自定义streambuf或ostream类的简化测试用例:#includeintmain(){std::streambuf*coutbuf=std::cout.rdbuf();std::ostream(coutbuf)使用g++编译:$g++--versiong++(Ubuntu4.4.1-4ubuntu8)4.4.1$g++-ofailreduced-case.cpp$./fail0x400c80:writingtocoutusingaseparateo

c++ - 在 std::map 中存储结构实例

我正在尝试将一些结构映射到其他一些实例,如下所示:templateclassComponent{public:typedefstd::mapinstances_map;instances_mapinstances;Component(){};Tadd(EntityIDid){T*t=newT();instances[id]=*t;return*t;};};然后我这样使用它:structUnitInfos{intowner_id;inthealth;floatx,y;};classLogicComponent:publicComponent{};问题是当它稍后检索数据时,像这样:comp

c++ - 消除对采用 std::functions 的函数的调用的歧义

下面的代码不能在gcc4.5上编译,因为对foo的调用不明确。消除歧义的正确方法是什么?#include#includeusingnamespacestd;voidfoo(std::functiont){t(1,2);}voidfoo(std::functiont){t(2);}intmain(){foo([](inta,intb){cout 最佳答案 最好的方法是显式创建一个std::function正确类型的对象,然后将该对象传递给函数:std::functionfunc=[](inta,intb){cout或内联:foo(st

C++ ifstream 函数和字段分隔符

对于这个程序,我只在shell脚本中使用了来自数据文件的字段分隔符。但我正在尝试使用标准库函数ifstream()从数据文件中读入。唯一的问题是我这样获取数据A:KT5:14:行政办公table:这是一个哈希表,我需要将数据结构和交易类型行中的值分开。我一直在网上四处寻找,并没有发现太多关于字段分隔符的信息,而且我发现的内容非常困惑。接下来的问题是,有没有办法用ifstream函数设置字段分隔符,或者我应该使用另一个标准库i/o函数?谢谢。 最佳答案 @SteveTownsend已经指出了一种可能性。如果您更喜欢使用operator

c++ - 在 C++ 中将 uint8_t* 转换为 std::string?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:HowtoConvertByte*tostd::stringinC++?我在嵌入式设备上尝试接收消息。此消息由constuint8_t*data及其长度size_tlen给出。现在我需要一个std::string来输出我的数据。

c++ - "1.$"是什么意思?

我在VisualC++2010中使用cout打印一个变量,它显示“1.$”。什么意思?Google不允许使用$进行搜索,因此我无法找到其含义。编辑:代码是这样的:doublefunc(...);std::cout我没有修改cout的默认值 最佳答案 它是一个无穷大的值,精度设置得很小:#include#includeintmain(){std::cout::infinity()::quiet_NaN()()::infinity()::quiet_NaN()这应该打印:1.#INF1.#QNAN1.$1.$编辑:来自@ZoogieZo

c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'

代码A:vector::const_reverse_iteratorrcit;vector::const_reverse_iteratortit=v.rend();for(rcit=v.rbegin();rcit!=tit;++rcit)cout代码B:vector::const_reverse_iteratorrcit;for(rcit=v.rbegin();rcit!=v.rend();++rcit)coutCODEA工作正常但是为什么代码B通过错误:DEVC++\vector_test.cpp在'rcit!=std::vector::rend()与_Tp=int,_Alloc=s

c++ - 将 vector<string> 转换为 vector<double> 的便捷方法

除了使用变换算法和istringstream之外,C++中是否有任何方便的方法将vector转换为vector?非常感谢您的帮助! 最佳答案 lexical_cast相当“方便”。for(size_ti=0;i(vec[i]));}当然,使用std::transform会变得更加有趣:std::transform(strings.begin(),strings.end(),std::back_inserter(doubles),boost::lexical_cast);//Notethetwotemplatearguments!at