标签: stringstream

使用std :: stringstream等效%02d?

我想输出一个std::stringstream等价格为printf's 的整数%02d.是否有更简单的方法来实现这一点:

std::stringstream stream;
stream.setfill('0');
stream.setw(2);
stream << value;
Run Code Online (Sandbox Code Playgroud)

是否有可能将某种格式标志流式传输给stringstream类似(伪代码)的东西:

stream << flags("%02d") << value;
Run Code Online (Sandbox Code Playgroud)

c++ formatting stringstream

58
推荐指数
4
解决办法
7万
查看次数

为什么不允许复制stringstream?

int main()
{
   std::stringstream s1("This is my string.");
   std::stringstream s2 = s1; // error, copying not allowed
}
Run Code Online (Sandbox Code Playgroud)

我找不到为什么我不能复制stringstream的原因.你能提供一些参考吗?

c++ stringstream

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

为什么stringstream >>在失败时改变目标值?

来自Stroustrup的TC++ PL,第3版,第21.3.3节:

如果我们尝试读入变量v并且操作失败,则v的值应该保持不变(如果v是istream或ostream成员函数处理的类型之一,则它不会改变).

以下示例似乎与上述引用相矛盾.基于上面的引用,我期待v的值保持不变 - 但它会变为零.对这种明显的矛盾行为有什么解释?

#include <iostream>
#include <sstream>

int main( )
{
    std::stringstream  ss;

    ss  << "The quick brown fox.";

    int  v = 123;

    std::cout << "Before: " << v << "\n";

    if( ss >> v )
    {
        std::cout << "Strange -- was successful at reading a word into an int!\n";
    }

    std::cout << "After: " << v << "\n";

    if( ss.rdstate() & std::stringstream::eofbit  ) std::cout << "state: eofbit\n";
    if( ss.rdstate() & std::stringstream::failbit ) std::cout << "state: failbit\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ state stringstream cin

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

为什么`std :: stringstream :: stringstream(std :: string &&)`不存在?

我希望stringstream有一个构造函数窃取其初始内容string&&.STL中通常不存在这样的种间"移动构造函数"吗?如果没有,为什么不呢?

c++ stringstream rvalue-reference move-semantics c++17

48
推荐指数
2
解决办法
2145
查看次数

将串流内容写入流中

我目前正在使用std::ofstream如下:

std::ofstream outFile;
outFile.open(output_file);
Run Code Online (Sandbox Code Playgroud)

然后我尝试将std::stringstream对象传递给outFile如下:

GetHolesResults(..., std::ofstream &outFile){
  float x = 1234;
  std::stringstream ss;
  ss << x << std::endl;
  outFile << ss;
}
Run Code Online (Sandbox Code Playgroud)

现在我只outFile包含垃圾:"0012E708"重复了一遍.

GetHolesResults我可以写

outFile << "Foo" << std:endl; 
Run Code Online (Sandbox Code Playgroud)

并且它将正确输出outFile.

关于我做错了什么的任何建议?

c++ parameters stl stringstream ofstream

47
推荐指数
2
解决办法
5万
查看次数

C#中的StringStream

我希望能够从我创建的类派生出一个字符串Stream.具体来说,我希望能够编写如下代码:

void Print(Stream stream) {
    // Some code that operates on a Stream.
}

void Main() {
    StringStream stream = new StringStream();
    Print(stream);
    string myString = stream.GetResult();
}
Run Code Online (Sandbox Code Playgroud)

我可以创建一个叫做的类StringStream吗?或者这样的课程已经可用?

更新:在我的示例中,该方法Print在第三方外部DLL中提供.如您所见,Print期望的论点是Stream.打印到之后Stream,我希望能够将其内容检索为字符串.

.net c# stringstream

45
推荐指数
5
解决办法
10万
查看次数

清空stringstream的最佳方法是什么?

其中一种可能性是:

somestringstream.str("");
Run Code Online (Sandbox Code Playgroud)

但它最优化吗?有没有办法保留stringstream内部缓冲区,以便跟随运算符<<()调用不需要再次保留内存?

c++ stl stringstream

40
推荐指数
1
解决办法
4万
查看次数

如何将文件内容读入istringstream?

为了提高从文件读取的性能,我试图将大(几MB)文件的整个内容读入内存,然后使用istringstream来访问信息.

我的问题是,哪个是读取此信息并将其"导入"到字符串流中的最佳方法?这种方法的一个问题(参见下文)是在创建字符串流时,缓冲区被复制,内存使用量增加一倍.

#include <fstream>
#include <sstream>

using namespace std;

int main() {
  ifstream is;
  is.open (sFilename.c_str(), ios::binary );

  // get length of file:
  is.seekg (0, std::ios::end);
  long length = is.tellg();
  is.seekg (0, std::ios::beg);

  // allocate memory:
  char *buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);

  // create string stream of memory contents
  // NOTE: this ends up copying the buffer!!!
  istringstream iss( string( buffer ) );

  // delete temporary buffer
  delete [] buffer;

  // close …
Run Code Online (Sandbox Code Playgroud)

c++ memory optimization stream stringstream

37
推荐指数
2
解决办法
9万
查看次数

如何检查C++字符串是否为int?

当我使用时getline,我会输入一串字符串或数字,但我只想让while循环输出"word",如果它不是数字.那么有没有办法检查"word"是否是一个数字?我知道我可以atoi()用于C字符串,但字符串类的字符串怎么样?

int main () {
  stringstream ss (stringstream::in | stringstream::out);
  string word;
  string str;
  getline(cin,str);
  ss<<str;
  while(ss>>word)
    {
      //if(    )
        cout<<word<<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ string types stringstream

37
推荐指数
5
解决办法
12万
查看次数

如何处理最后一个逗号,当用逗号分隔字符串时?

可能重复:
请勿在最后一个数字后打印空格
使用逗号C++打印列表

#include <vector>
#include <iostream>
#include <sstream>
#include <boost/foreach.hpp>
using namespace std;

int main()
{
   vector<int> VecInts;

   VecInts.push_back(1);
   VecInts.push_back(2);
   VecInts.push_back(3);
   VecInts.push_back(4);
   VecInts.push_back(5);

   stringstream ss;
   BOOST_FOREACH(int i, VecInts)
   {
      ss << i << ",";
   }

   cout << ss.str();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

这打印出来:1,2,3,4,5, 但是我想要:1,2,3,4,5

我怎样才能以优雅的方式实现这一目标?

我看到我对"优雅"的含义有些困惑:例如,我的循环中没有减慢"if-clause"的速度.想象一下向量中的100.000个条目!如果这就是你要提供的全部内容,我宁愿在完成循环后删除最后一个逗号.

c++ csv string boost stringstream

33
推荐指数
5
解决办法
2万
查看次数