C ++创建文本字符串和变量 [英] C++ create string of text and variables
问题描述
我正在尝试做一些非常简单的事情,但是,经过一个小时的搜索,我找不到合适的答案,所以我肯定错过了一些显而易见的事情。
I'm trying to do something very simple and yet, after an hour of so of searching a I can't find a suitable answer so I must be missing something fairly obvious.
我正在尝试动态创建用于ifstream的文件名。虽然我了解可以使用多种方法来执行此操作,但是我已经决定创建std :: string,并使用stringname.c_str转换为const。
I'm trying to dynamically create filenames for use with ifstream. Whilst I understand various methods are available of doing this, I have settled on creating a std::string, and the using stringname.c_str to convert to const.
问题但是,我需要使用变量和预定义的文本值的组合来创建字符串。我遇到编译器错误,因此这肯定是语法问题。
The problem is however that I need to create the string with a mix of variables and predefined text values. I'm getting compiler errors, so this must be a syntax issue.
伪
std::string var = "sometext" + somevar + "sometext" + somevar;
谢谢!
推荐答案
您是否考虑过使用字符串流?
Have you considered using stringstreams?
#include <string>
#include <sstream>
std::ostringstream oss;
oss << "sometext" << somevar << "sometext" << somevar;
std::string var = oss.str();
这篇关于C ++创建文本字符串和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!