C++;:字符串流提供了什么好处? 可以告诉我一些关于C++中使用字符串流的实例,即使用流插入和流提取算子输入和输出到字符串流?
,除了使用GCC 4.3.1之外,还可以使用字符串流来转换实现代码< >运算符> P>的任何东西。我没有检查以前版本的gcc 它们可以在任何可以使用正常流的地方使用 因此,在从文件中读取数据的情况下,您可能会从字符串流中读取数据C++;:字符串流提供了什么好处? 可以告诉我一些关于C++中使用字符串流的实例,即使用流插入和流提取算子输入和输出到字符串流?,c++,string,stringstream,C++,String,Stringstream,,除了使用GCC 4.3.1之外,还可以使用字符串流来转换实现代码< >运算符> P>的任何东西。我没有检查以前版本的gcc 它们可以在任何可以使用正常流的地方使用 因此,在从文件中读取数据的情况下,您可能会从字符串流中读取数据 void compile(std::istream& str) { CPlusPlusLexer lexer(str); CPlusPlusParser parser(lexer); BackEnd backend
void compile(std::istream& str)
{
CPlusPlusLexer lexer(str);
CPlusPlusParser parser(lexer);
BackEnd backend(parser);
backend.compile();
}
int main()
{
std::fstream file("Plop.cpp");
compile(file);
std::stringstream test("#include <iostream>\n int main() { std::cout << \"H World\n\";}");
compile(test);
}
void编译(std::istream&str)
{
cpluspluser-lexer(str);
cpluplusparser(lexer);
后端(解析器);
backend.compile();
}
int main()
{
std::fstream文件(“Plop.cpp”);
编制(文件);
std::stringstream test(“#include\n int main(){std::cout在创建消息时,我主要将它们用作内存缓冲区:
if(someVector.size() > MAX_SIZE)
{
ostringstream buffer;
buffer << "Vector should not have " << someVector.size() << " eleements";
throw std::runtime_error(buffer.str());
}
if(someVector.size()>MAX\u size)
{
ostringstream缓冲区;
你是说cin和cout?与C风格的getc和printf相反?@Vanessa:OP可能意味着std::stringstream
,(#include
)请记住,此解决方案仅适用于operator@fogo:而且,由于为类定义流式操作符比较常见,这并不是什么大问题。@Martin York的确,考虑到答案中的convert anything备注,我刚刚考虑了一个合适的注释。这是一个限制一个特定的实现(gcc)的定义。如果你通过链接找到与问题相关的bugzilla错误,你也会注意到该错误已被标记为已解决,互斥锁不再使用,那就太好了!我将编辑我的帖子,以便添加与gcc的某些版本相关的信息。
void compile(std::istream& str)
{
CPlusPlusLexer lexer(str);
CPlusPlusParser parser(lexer);
BackEnd backend(parser);
backend.compile();
}
int main()
{
std::fstream file("Plop.cpp");
compile(file);
std::stringstream test("#include <iostream>\n int main() { std::cout << \"H World\n\";}");
compile(test);
}
if(someVector.size() > MAX_SIZE)
{
ostringstream buffer;
buffer << "Vector should not have " << someVector.size() << " eleements";
throw std::runtime_error(buffer.str());
}
std::string MyObject::GenerateDumpPath()
{
using namespace std;
std::ostringstream dumpPath;
// add the file name
dumpPath << "\\myobject."
<< setw(3) << setfill('0') << uniqueFileId
<< "." << boost::lexical_cast<std::string>(state)
<< "_" << ymd.year
<< "." << setw(2) << setfill('0') << ymd.month.as_number()
<< "." << ymd.day.as_number()
<< "_" << time.hours()
<< "." << time.minutes()
<< "." << time.seconds()
<< ".xml";
return dumpPath.str();
}