标签: sstream

C++ stringstream返回额外的字符?

我一直在尝试使用C++ stringstream类来做一些相对简单的字符串操作,但是我遇到了get()方法的问题.出于某种原因,每当我按字符提取输出字符时,它会附加最后一个字母的第二个副本.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
   stringstream ss("hello");
   char c;

   while(!ss.eof()) {
      ss.get(c);
      cout << "char: " << c << endl;
   }
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是:

char: h
char: e
char: l
char: l
char: o
char: o
Run Code Online (Sandbox Code Playgroud)

任何帮助,你可以给我这个将不胜感激.

c++ string stream sstream

2
推荐指数
1
解决办法
3254
查看次数

用公共访问编译器错误重新声明了sstream

使用gcc5.4.0在大型项目上运行make时,我遇到了此错误。

/usr/include/c++/5/sstream:300:14: error: '__xfer_bufptrs' redeclared with 'public' access
      struct __xfer_bufptrs
             ^
/usr/include/c++/5/sstream:67:14: note: previously declared 'private' here
      struct __xfer_bufptrs;
Run Code Online (Sandbox Code Playgroud)

在我看来,编译器似乎有问题?由于问题出现在标准c ++库sstream中?这对我来说没有意义,我使用的是错误的编译器吗?

以下是错误消息所引用的代码片段:

1.)从第67行开始

class basic_stringbuf : public basic_streambuf<_CharT, _Traits>                                   
    {                                                                                                 
      struct __xfer_bufptrs;                                                                          
    public:                                                                                           
Run Code Online (Sandbox Code Playgroud)

2.)在第300行进行流式传输

#if _GLIBCXX_USE_CXX11_ABI                                                                            
      // This type captures the state of the gptr / pptr pointers as offsets                          
      // so they can be restored in another object after moving the string.                           
      struct __xfer_bufptrs                                                                           
      {                                                                                               
        __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)                          
        : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}                                       
        { …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors sstream

2
推荐指数
1
解决办法
1385
查看次数

在C++中使用sstream头文件

所以我试图利用istringstream来解析文本文件.我们的想法是按空间分解每一行,并根据子串做事.代码工作正常,除了两件事,它重复计算每一行的最后一个子字符串,当它完成读取文件时它会出错.我之前没有使用过sstream,所以任何见解都会有所帮助.

file.getline(str,80);

    while(!file.eof())

    {

        cout<<str<<endl;
        istringstream iss(str);
        while (iss)
            {
                iss >> sstr;
                cout << "Substring: " <<sstr << endl;
         }
        file.getline(str,80);   
    }
Run Code Online (Sandbox Code Playgroud)

c++ sstream

1
推荐指数
1
解决办法
1672
查看次数

使用sstream将std :: string转换为int

我正在尝试将任意长度的字符串转换为int,但到目前为止它只适用于有限长度的字符串.代码到目前为止:

long long convertToInt (std::string x){
   long long number;
   std::istringstream ss(x);
   ss >> number;
   return number;}
Run Code Online (Sandbox Code Playgroud)

x=100000000000000000000000001函数返回0.有人能解释为什么吗?谢谢.

c++ string type-conversion sstream

1
推荐指数
1
解决办法
135
查看次数

将单个char转换为std :: string prepends\x01

我正在尝试从具有std::stringas键的无序映射中接收值,其中一些字符串只包含一个字符.我的所有输入都来自于std::stringstream我从中获取每个值并将其转换为char,然后将其转换为字符串使用
std::string result {1, character};,根据文档此答案,这似乎是有效的.

但是,当我这样做时,字符串前置\ x01(对应于值1).这使得在地图中找不到字符串.我的调试器还确认字符串大小为2,值为"\ x01H".

为什么会发生这种情况,我该如何解决?

#include <iostream>
#include <sstream>
#include <unordered_map>

int main()
{
    const std::unordered_map<std::string, int> map = { {"H", 1}, {"BX", 2} };

    std::stringstream temp {"Hello world!"};
    char character = static_cast<char>(temp.get());  // character is 'H'
    std::string result {1, character};               // string contains "\x01H"

    std::cout << result << " has length " << result.size() << std::endl; // H has length 2
    std::cout << map.at(result) << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ string std sstream c++17

1
推荐指数
1
解决办法
226
查看次数

(C++)std :: istringstream从字符串到双精度读取最多6位数字

乡亲!我一直在努力解决这个问题,到目前为止我还没有找到任何解决方案.

在下面的代码中,我使用数字初始化一个字符串.然后我使用std :: istringstream将测试字符串内容加载到double中.然后我提出两个变量.

#include <string>
#include <sstream>
#include <iostream>

std::istringstream instr;

void main()
{
    using std::cout;
    using std::endl;
    using std::string;

    string test = "888.4834966";
    instr.str(test);

    double number;
    instr >> number;

    cout << "String test:\t" << test << endl;
    cout << "Double number:\t" << number << endl << endl;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

当我运行.exe时,它看起来像这样:

字符串测试:888.4834966
双号888.483
按任意键继续...

字符串有更多的数字,看起来像std :: istringstream只加载了6中的10个.如何将所有字符串加载到double变量中?

c++ sstream istringstream

0
推荐指数
1
解决办法
1303
查看次数