简体   繁体   English

如何返回包含string / int变量的字符串

[英]How to return string that contains string/int variables

For example, if I have this little function: 例如,如果我有这个小功能:

string lw(int a, int b) {    
    return "lw $" + a + "0($" + b + ")\n";
}

....and make the call lw(1,2) in my main function I want it to return "lw $1, 0($2)" . ....并在我的主函数中调用lw(1,2)我希望它返回"lw $1, 0($2)"

But I keep getting an error: invalid operands of types 'const char*' and 'const char [11]' to binary 'operator+' 但我不断收到错误: invalid operands of types 'const char*' and 'const char [11]' to binary 'operator+'

What am I doing wrong? 我究竟做错了什么? I pretty much copied an example from class, and changed it to suit my function. 我几乎从类中复制了一个例子,并将其更改为适合我的功能。

You are trying to concatenate integers to strings, and C++ can't convert values of such different types. 您正在尝试将整数连接到字符串,并且C ++无法转换此类不同类型的值。 Your best bet is to use std::ostringstream to construct the result string: 最好的办法是使用std::ostringstream构造结果字符串:

#include <sstream>

// ...

string lw(int a, int b)
{
    ostringstream os;
    os << "lw $" << a << "0($" << b << ")\n";
    return os.str();
}

If you have Boost , you can use Boost.Lexical_cast : 如果你有Boost ,你可以使用Boost.Lexical_cast

#include <boost/lexical_cast.hpp>

// ...

string lw(int a, int b)
{
    return
        string("lw $") +
        boost::lexical_cast<std::string>(a) +
        string("0($") +
        boost::lexical_cast<std::string>(b) +
        string(")\n");
}

And now with C++11 and later there is std::to_string : 现在使用C ++ 11及更高版本的std::to_string

string lw(int a, int b)
{
    return
        string("lw $") +
        std::to_string(a) +
        string("0($") +
        std::to_string(b) +
        string(")\n");
}
#include <sstream>

string lw(int a, int b) {    
    std::string s;
    std::stringstream out;
    out << "lw $" << a << "0($" << b << ")" << endl;
    s = out.str();
    return s;
}

Use an ostringstream: 使用ostringstream:

#include <sstream>
...
string lw(int a, int b) {
    std::ostringstream o;
    o << "lw $" << a << "0($" << b << ")\n";
    return o.str();
}

You cannot add string literals (like "hello") to integers. 您不能将字符串文字(如“hello”)添加到整数。 That is what compiler says to you. 这就是编译器对你说的。 This is partial answer to your question. 这是对您的问题的部分答案。 Please see how to accomplish what you want in another posts. 请参阅如何在其他帖子中完成您想要的内容。

To understand this question, you have to know that in C++, string literals like "lw $" are treated as const char[] as inherited from the C language. 要理解这个问题,你必须知道在C ++中,像"lw $"这样的字符串文字被视为继承自C语言的const char[] However this means that you only get operators that are defined for arrays, or in this case the array-degrades-to-pointer case. 但是,这意味着您只获得为数组定义的运算符,或者在这种情况下是数组降级到指针的情况。

So what happens is you have a string literal, and then add an integer to it, creating a new pointer. 所以会发生什么是你有一个字符串文字,然后添加一个整数,创建一个新的指针。 Then you attempt to add another string literal which again degrades to char* . 然后,您尝试添加另一个字符串文字,再次降级为char* You can't add two pointers together, which then generates the error you're seeing. 您不能将两个指针添加到一起,然后生成您正在看到的错误。

You're trying to format integers into a string format with some delimiting text. 您正在尝试将整数格式化为带有一些分隔文本的字符串格式。 In C++ the canonical way of doing that is with stringstreams: 在C ++中,使用stringstreams的规范方法是:

#include <sstream>

string lw(int a, int b)
{
    std::ostringstream os;
    os << "lw $" << a << "0($" << b << ")\n";
    return os.str();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM