我正在尝试将字符串流中的数据存储到向量中.我可以成功地这样做但它忽略了字符串中的空格.如何使空间也被推入向量?
谢谢!
代码存根:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
stringstream s;
string line = "HELLO HELLO\0";
stringstream stream(line);
unsigned char temp;
vector<unsigned char> vec;
while(stream >> temp)
vec.push_back(temp);
for (int i = 0; i < vec.size(); i++)
cout << vec[i];
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 看一些旧代码,我们有很多类似的东西:
// This is dumb
string do_something(int in)
{
stringstream out;
try
{
out << std::fixed << in;
}
catch(std::exception &e)
{
out << e.what();
}
return out.str();
}
// Can't we just do this? Can this ever fail?
string do_something_better(int in)
{
stringstream out;
out << std::fixed << in;
return out.str();
}
Run Code Online (Sandbox Code Playgroud)
当stringstream读取一个原语时,它是否会抛出一个异常?读字符串怎么样?
在我正在进行的项目中,我链接到一个专有的动态库.一旦我运行库的初始化函数,记录和打印数字的行为就会改变.
逗号每三位小数插入一次.IE浏览器.
cout << 123456789 << endl
Run Code Online (Sandbox Code Playgroud)
用于打印出来123456789,现在打印出来123,456,789.这非常令人讨厌,因为这种行为不是我想要的.
这个问题不仅在我正在编译的二进制文件中很明显,而且还出现在我链接到它的所有couts和stringstreams库中.
我在调用initialize函数后尝试使用这行代码
setlocale(LC_ALL,"C");
Run Code Online (Sandbox Code Playgroud)
认为它可能会将我的语言环境重置为默认值; 但无济于事.逗号坚持!!
这段代码:
std::cout.imbue(std::locale("C"));
Run Code Online (Sandbox Code Playgroud)
用于重置我的couts每个stringstream应用程序的语言环境.但是,我真的需要调用我链接到的每个库中的imbue每个stringstream声明吗?有些库是专有的,我实际上无法更改其源代码.
必须有办法将语言环境重置为"C"全局?
我想做一个struct,其中一个成员是std::stringstream类型.我正在使用C++ 11,并且根据http://www.cplusplus.com/reference/sstream/stringstream/operator=/我可以做到.
这是我的代码:
struct logline_t
{
stringstream logString; /*!< String line to be saved to a file (and printed to cout). */
ElogLevel logLevel; /*!< The \ref ElogLevel of this line. */
timeval currentTime; /*!< time stamp of current log line */
logline_t& operator =(const logline_t& a)
{
logString = a.logString;
logLevel = a.logLevel;
currentTime = a.currentTime;
return *this;
}
};
Run Code Online (Sandbox Code Playgroud)
它没有编译,因为我收到此错误:
error: use of deleted function ‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)’
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它不起作用.我也试过logString = move(a.logString); …
我有一个网络客户端,其请求方法需要一个std::streambuf*.这个方法是通过将boost::iostreams::copy它发送到一个自定义std::streambuf类来实现的,该类知道如何将数据写入网络API,这很有效.这意味着我可以将文件流式传输到请求中,而无需将其全部读入内存.
但是,在某些情况下,必须发送不在文件中的大块数据,因此我包含了一个带字符串的重载.为了避免重复流中的所有网络代码,我应该设置一个streambuf代表字符串并调用另一个方法.我能弄清楚这项工作的唯一方法是:
std::istringstream ss(data);
send(ss.rdbuf());
Run Code Online (Sandbox Code Playgroud)
不幸的是,istringstream制作数据的副本,在某些情况下是几兆字节.当然,在一般情况下,它非常有意义,如果你将const引用交给某个对象,你不希望该对象假设它可以继续使用该引用.
我用以下方法解决了这个问题:
struct zerocopy_istringbuf
: public std::stringbuf
{
zerocopy_istringbuf(std::string const* s)
: std::stringbuf(std::ios::in)
{
char* p = const_cast<char*>(s->c_str());
setg(p, p, p + s->length());
}
};
...
send(&zerocopy_istringbuf(data));
Run Code Online (Sandbox Code Playgroud)
这似乎工作得很好,但我想知道它是否真的有必要.为什么没有std::istringstream过载std::string const *?有一个更好的方法吗?
我用于简单字符串拆分的常见代码如下所示:
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
Run Code Online (Sandbox Code Playgroud)
有人提到这会默默地"吞噬"发生的错误std::getline.当然,我同意这种情况.但是我想到了,在实践中我可能会出现什么问题,我需要担心.基本上这一切归结为:
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
if(/* what error can I catch here? */) {
// *** How did we get here!? ***
}
return elems;
}
Run Code Online (Sandbox Code Playgroud)
A stringstream由a支持string,因此我们不必担心与从文件读取相关的任何问题.此处没有类型转换,因为getline只需读取直到它看到行分隔符或EOF.所以我们不能得到任何类似的错误boost::lexical_cast …
我想尝试使用stringstream进行分配,但我对它的工作原理有点困惑.我做了一个快速搜索,但找不到任何能回答我问题的东西.
假设我有一个动态大小的流,我怎么知道何时停止写入变量?
string var = "2 ++ asdf 3 * c";
stringstream ss;
ss << var;
while(ss){
ss >> var;
cout << var << endl;
}
Run Code Online (Sandbox Code Playgroud)
我的输出将是:
2
++
asdf
3
*
c
c
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我最后得到额外的'c',特别是因为_M_in_cur = 0x1001000d7""
我正在创建一个函数来返回十进制和整数位数,并typename使用sstreams 将插入的数字转换为字符串.
但是,转换为字符串时的数字用科学记数法表示,这对于计算正常数字中的数字位数没有用.如何在下面的函数中阻止这种情况发生?
enum { DECIMALS = 10, WHOLE_NUMBS = 20, ALL = 30 };
template < typename T > int Numbs_Digits(T numb, int scope)
{
stringstream ss(stringstream::in | stringstream::out);
stringstream ss2(stringstream::in | stringstream::out);
unsigned long int length = 0;
unsigned long int numb_wholes;
ss2 << (int) numb;
numb_wholes = ss2.str().length();
ss2.flush();
bool all = false;
switch (scope) {
case ALL:
all = true;
case DECIMALS:
ss << numb;
length += ss.str().length() - (numb_wholes + 1); …Run Code Online (Sandbox Code Playgroud) 如何连接两个字符串流?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "types.h"
int main () {
char dest[1020] = "you";
char source[7] = "baby";
stringstream a,b;
a << source;
b << dest;
a << b; /*HERE NEED CONCATENATE*/
cout << a << endl;
cout << a.str() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
两次尝试中的输出如下:
0xbf8cfd20
baby0xbf8cfddc
Run Code Online (Sandbox Code Playgroud)
期望的输出是babyyou.
我正在尝试从流中读取单个字符.使用以下代码,我得到一个"模糊过载"编译器错误(GCC 4.3.2 和4.3.4).我做错了什么?
#include <iostream>
#include <sstream>
int main()
{
char c;
std::istringstream("a") >> c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
备注:
int,double)正在工作std::istringstream iss("a"); iss >> c,编译器不会给出错误