我正在读取大小为5的字符数组的输入,
stringstream ss;
char a[5];
if (!ss.read(a, 5))
{
// throw exception
}
if (!ss.get(a, 5))
{
// throw exception
}
Run Code Online (Sandbox Code Playgroud)
这两个功能似乎都有效,有什么区别吗?
我有一个先前声明的char c[64];,我正在尝试查看管道输出的第一个字:
read(pipe_replacement_to_main[READ_END], c, BUF_SIZE);
istringstream response_stream(string(c));
string response_string;
getline(response_stream, response_string, ' ');
Run Code Online (Sandbox Code Playgroud)
gcc在第四行给了我以下内容:
error: no matching function for call to ‘getline(std::istringstream (&)(std::string), std::string&, char)’
Run Code Online (Sandbox Code Playgroud)
我甚至无法弄清楚它是如何调用该函数的.我是否声明了istringstream错误?
我的代码看起来像:
template <typename type> void deserialize_element(type* result) {
//...
if /*...*/
else stringstream(line) >> *result;
}
Run Code Online (Sandbox Code Playgroud)
MSVC编译没有问题,但GCC给出:
error: ambiguous overload for 'operator>>' in 'std::basic_stringstream<char>(((const std::basic_stringstream<char>::__string_type&)((const std::basic_stringstream<char>::__string_type*)(& line))), std::operator|((std::_Ios_Openmode)16u, (std::_Ios_Openmode)8u)) >> * result'
/usr/include/c++/4.5/istream:120:7: note: candidates are: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
/usr/include/c++/4.5/istream:124:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] …Run Code Online (Sandbox Code Playgroud) 在创建一个简单的异常类扩展(我可以更容易地构造错误消息)时,我将错误分解为以下简单代码:
#include <sstream>
#include <string>
class myCout {
public:
std::stringstream ssOut; // Removing this gets rid of error
template <typename T> myCout& operator << (const T &x) {
// Do some formatting
return *this;
}
};
class myErr : public myCout {
public:
using myCout::operator<<;
};
int main(int argc, const char* argv[]) {
throw myErr() << "ErrorMsg" << 1;
myCout() << "Message Will Be Formatted";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,会产生以下错误:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\sstream(724): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot …Run Code Online (Sandbox Code Playgroud) 我无法从文档中看出它是如何std::stringstream.read()工作的.它是否消耗流?
换一种说法:
std::stringstream ss;
char buffer[6];
ss << "Hello world!";
ss.read(buffer, 6);
std::cout << ss.str(); // Is this "Hello world!" or just "world!"
Run Code Online (Sandbox Code Playgroud) 我在TopCoder中做了一个图形问题,不知怎的,我的程序仍然输出错误的答案,即使我认为一切都应该没问题.我花了几个小时的时间在我的逻辑中寻找错误,事实证明,问题在于其他地方.这是一段代码,我有一个问题:
int x, y;
stringstream ssx;
stringstream ssy;
for (int i = 0; i < connects.size(); i++){
neighbours.push_back(vector<int> ());
edges_cost.push_back(vector<int> ());
ssx.str(connects[i]);
ssy.str(costs[i]);
while (ssx >> x && ssy >> y){
neighbours[i].push_back(x);
edges_cost[i].push_back(y);
}
// The problem lied here. Apparently without these 2 lines
// the program outputs wrong answer. Why?
ssx.clear();
ssy.clear();
}
Run Code Online (Sandbox Code Playgroud)
正如您在评论中看到的那样,我设法解决了这个问题.但我不知道为什么我需要清除那些串流.如果我不这样做,到底发生了什么?
我是Winapi的初学者,我正在尝试将wstringstream转换为LPCWSTR(在WM_PAINT内):
wstringstream ws;
ws << "my text" << endl;
LPCWSTR myWindowOutput = ws.str().c_str();
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 150, 305, myWindowOutput, 10);
Run Code Online (Sandbox Code Playgroud)
它只生产垃圾,有人可以帮忙吗?谢谢.
在像这样的一段代码中
std::string s("random;string;");
std::stringstream s_stream(s);
Run Code Online (Sandbox Code Playgroud)
确实s_stream复制了s?
我的意思是,如果在创建s_stream之后我修改了会发生什么s?
我正在尝试实现一种从文本文件中读取输入以更轻松地加载不同坐标集的方法,但是我遇到了一个我不理解的错误,stringstream一旦其中一行是我的对象将停止接收字符串生病格式.
在我的输出中,您可以看到字符串在打印出来时仍然完好无损,然后它被放入stringstream下一行,但是在一个格式不正确的字符串之后,stringstream当我将其打印出来时,停止包含任何内容.
这里发生了什么?
输出:
这是文本文件的样子:
方法代码:
ifstream pathfile(p.string());
cout << "Path file opened successfully.\n\n";
string line;
stringstream ss;
int x, y;
char comma,direction;
//Iterate all lines in the file
while(getline(pathfile,line)){
//remove all spaces from line
line.erase(remove(line.begin(), line.end(), ' '), line.end());
//skip comments and blank lines
if(line.c_str()[0] == '#' || line.empty()) continue;
//parse remaining lines
ss.str(string()); //clear stringstream
cout <<"LINE: "<<line<<endl;
ss << line;
cout <<"SS: "<<ss.str()<<endl;
if(ss >> x >> comma >> y >> …Run Code Online (Sandbox Code Playgroud) 我正在阅读《示例的SFML游戏开发》一书,但我并没有真正理解这句话的含义。我从未见过这样的事情
void Anim_Directional::ReadIn(std::stringstream& l_stream){
l_stream >> m_frameStart >> m_frameEnd >> m_frameRow
>> m_frameTime >> m_frameActionStart >> m_frameActionEnd;
}
Run Code Online (Sandbox Code Playgroud)