我从昨天开始学习C++并且正在使用这个文档:http://www.cplusplus.com/files/tutorial.pdf(第32页).我在文档中找到了一个代码,然后运行它.我尝试输入Rs 5.5的价格和一个整数的数量,输出为0.我尝试输入5.5和6,输出是正确的.
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price = 0;
int quantity = 0;
cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题:mystring命令究竟做了什么?引自文件:
"在这个例子中,我们间接从标准输入中获取数值.不是直接从标准输入中提取数值,而是从标准输入(cin)获取行到字符串对象(mystr),然后我们提取整数此字符串中的值为int(quantity)类型的变量."
我的印象是该函数将采用字符串的组成部分并将其用作输入.
(我不知道如何在这里提问.我也是编程的新手)谢谢.
当我调用toString()方法时,我试图返回一些信息,包括一个整数和一些浮点数.我了解到ostringstream工作得很好,但是当一遍又一遍地调用包含这个方法的类时,信息会堆叠到我之前的输出上.这是我的代码
ostringstream int_buffer, float_buffer, float_buffer2;
Run Code Online (Sandbox Code Playgroud)
那是在我班上开始介绍的
string toString()
{
int_buffer << on_hand;
float_buffer << price;
float_buffer2 << generated_revenue;
string stron_hand = int_buffer.str();
string strprice = float_buffer.str();
string strrev = float_buffer2.str();
string output = "Product name: " + description + " Units left: " + stron_hand + " Price: " + strprice + " Revenue: $" + strrev;
return output;
}
Run Code Online (Sandbox Code Playgroud)
我知道我的编码很糟糕,我还是比较新的,但我输出的一个例子是,
"产品名称:电影票单位:49价格:9.99收入:9.99美元"
"产品名称:电影票单位:4926价格:9.999.99收入:$ 9.99239.76"
第二个应该显示的位置
"产品名称:电影票单位:26价格:9.99收入:$ 239.76"
我知道这只是一个更新的问题,但那就是我迷失的地方.
我试图找出如何使用" sstream"和C++ 解析这个字符串
它的格式是:"string,int,int".
我需要能够将包含IP地址的字符串的第一部分分配给std :: string.
以下是此字符串的示例:
std::string("127.0.0.1,12,324");
Run Code Online (Sandbox Code Playgroud)
然后我需要获得
string someString = "127.0.0.1";
int aNumber = 12;
int bNumber = 324;
Run Code Online (Sandbox Code Playgroud)
我会再次提到我不能使用boost库,只是sstream:-)
谢谢
我是c ++的新手,请帮我弄清楚这有什么问题
string c;
stringstream out; //aggregate 'std::stringstream out' has incomplete type and cannot be //defined
out << it->second;
out << end1;//'end1' was not declared in this scope
c = out.str();
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
#include <iostream>
#include <sstream>
int main()
{
std::stringstream ss;
ss << "12345";
unsigned short s;
ss >> s;
ss << "foo";
std::cout << std::boolalpha
<< "\nss.eof() = " << ss.eof()
<< "\nss.good() = " << ss.good()
<< "\nss.bad() = " << ss.bad()
<< "\nss.fail() = " << ss.fail()
<< "\nss.str() = " << ss.str();
}
Run Code Online (Sandbox Code Playgroud)
clang ++ trunk打印出以下结果:
Run Code Online (Sandbox Code Playgroud)ss.eof() = true ss.good() = false ss.bad() = false ss.fail() = false ss.str() = 12345
g ++ trunk打印以下结果:
Run Code Online (Sandbox Code Playgroud)ss.eof() = …
我有一个C++类MyObject,我希望能够像对待osstream一样提供这些数据(但与直接sstream不同,将传入数据格式化为特殊方式).我似乎无法弄清楚如何为MyObject重载操作符来吃掉给它的输入.
class MyObject {
public:
ostringstream s;
FEEDME
};
int main() {
MyObject obj;
obj.FEEDME << "Hello" << 12345;
// I want obj.s == ":Hello::12345:"
}
Run Code Online (Sandbox Code Playgroud)
我想要它所以喂入的每个项目被包围:
所以在给定的例子中,s =":Hello :: 12345"应该是最终的结果.我的问题是,我怎么能告诉对象什么时候<<something,把:贴在东西周围.
这可能吗?
我有这个代码,
int main()
{
std::string st;
std::stringstream ss;
ss<<"hej hej med dig"<<std::endl;
std::getline(ss,st,' ');
std::cout <<"ss.rdbuf()->str() : " << ss.rdbuf()->str();
std::cout <<"ss.rdbuf() : " << ss.rdbuf();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给我这个输出
ss.rdbuf() - > str():hej hej med dig
ss.rdbuf():hej med dig
但那是为什么呢?是因为运算符<str()的ostreams定义给了我不同的输出.在我看来,即使我使用了getline,输出应该是相同的.
我想直接读入一个代码如下的字符串:
std::string myString(( std::ostringstream() << myInt << " "
<< myFloat << " "
<< std::boolalpha << myBool ).str());
Run Code Online (Sandbox Code Playgroud)
但VS2012给了我一个basic_ostream没有str()方法的投诉.
有没有办法用匿名字符串流来做到这一点?
我对C++和Open GL非常陌生,我一直在尝试在场景中显示3D对象.它与一个工作正常,但当我试图改变我的代码添加一秒,我的代码关于显示相机位置的HUD文本开始给出错误.显示上面的错误,它显然在sstream文件中(#include).我试过四处搜寻并寻求帮助,但没有任何帮助/我理解.当我注释掉#include行和使用它的代码时,我得到了类似的说法"错误C2143:语法错误:缺少';' 在我的main.cpp文件中'使用'之前.
我正在运行Visual Studio 2010,我甚至试图将整个事情重新打开,然后将代码复制到新项目中.非常感谢帮助.
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include "SceneObject.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
//#include <cmath>
//#include <limits>
//#include <cstdlib>
using namespace std;
Run Code Online (Sandbox Code Playgroud)
...
stringstream ss;
ss << "Camera (" << cam.pos.x << ", " << cam.pos.y << ", " << cam.pos.z << ")";
glClear(GL_DEPTH_BUFFER_BIT);
outputText(-1.0, 0.5, ss.str());
Run Code Online (Sandbox Code Playgroud)
...
#ifndef SCENEOBJECT_H
#define SCENEOBJECT_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
struct point3D {
float x;
float y;
float …Run Code Online (Sandbox Code Playgroud) 我正在使用模板函数,我正在传递,我可能会将各种类的实例发送到字符串流.我该怎么做才能确保它继续有效?
让我更具体一点,我在哪里定义行为?是否有一些成员应该在每个类上发送到字符串流,如果我在一些增强或扩展现有的String流(我正在考虑构建一个继承自sstream的类并重载<<运算符来处理所有可能的班)?
我甚至无法找到相关文档,所以即使链接到更多资源也会有所帮助.