C++ 创建文本字符串和变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10219225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:45:33  来源:igfitidea点击:

C++ create string of text and variables

c++stringvariablesstd

提问by Hyman Farrow

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.

我正在尝试做一些非常简单的事情,但是,经过一个小时的搜索后,我找不到合适的答案,所以我必须遗漏一些相当明显的东西。

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.

我正在尝试动态创建用于 ifstream 的文件名。虽然我知道有各种方法可以做到这一点,但我已经决定创建一个 std::string,并使用 stringname.c_str 转换为 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.

然而,问题是我需要创建混合变量和预定义文本值的字符串。我收到编译器错误,所以这一定是语法问题。

Pseudo

std::string var = "sometext" + somevar + "sometext" + somevar;

Thanks!

谢谢!

回答by mgiuffrida

Have you considered using stringstreams?

您是否考虑过使用字符串流?

#include <string>
#include <sstream>

std::ostringstream oss;
oss << "sometext" << somevar << "sometext" << somevar;
std::string var = oss.str();

回答by David Schwartz

std::string var = "sometext" + somevar + "sometext" + somevar;

This doesn't work because the additions are performed left-to-right and "sometext"(the first one) is just a const char *. It has no operator+to call. The simplest fix is this:

这不起作用,因为添加是从左到右执行的,并且"sometext"(第一个)只是一个const char *. 它没有operator+打电话。最简单的修复是这样的:

std::string var = std::string("sometext") + somevar + "sometext" + somevar;

Now, the first parameter in the left-to-right list of +operations is a std::string, which has an operator+(const char *). That operator produces a string, which makes the rest of the chain work.

现在,从左到右的+操作列表中的第一个参数是 a std::string,它有一个operator+(const char *). 该运算符生成一个字符串,使链的其余部分工作。

You can also make all the operations be on var, which is a std::stringand so has all the necessary operators:

您还可以将所有操作都设置为 on var,它是 a std::string,因此具有所有必要的运算符:

var = "sometext";
var += somevar;
var += "sometext";
var += somevar;

回答by David Rinck

In C++11 you can use std::to_string:

在 C++11 中,您可以使用std::to_string

std::string var = "sometext" + std::to_string(somevar) + "sometext" + std::to_string(somevar);  

回答by Yury

See also boost::format:

另见boost::format

#include <boost/format.hpp>

std::string var = (boost::format("somtext %s sometext %s") % somevar % somevar).str();

回答by Shocker

You can also use sprintf:

您还可以使用 sprintf:

char str[1024];
sprintf(str, "somtext %s sometext %s", somevar, somevar);

回答by alkino

The new way to do with c++20 is using format.

处理 c++20 的新方法是使用format

#include <format>

auto var = std::format("sometext {} sometext {}", somevar, somevar);