如何使用双引号括起 return 值

How to at a return value with double quote character enclosing it

我正在做 C++ 作业,我们正在创建重载函数。教师希望将值包含在 "" 中。我能够完成那部分,但他澄清说他希望在 main() 函数中的 cout 语句之前添加 ""s。

下面的代码运行并产生正确的输出,除了双引号的应用。我不知道如何在重载函数中应用双引号。我尝试了 Google 中的几个不同示例,但到目前为止运气不好。

#include<iostream>
#include<string>
    
using namespace std;
    
int iQuote(int);
double iQuote(double);
string iQuote(string);
        
int x = 4;
double y = 8.2;
string name = "Dan";
    
int main()
{
    cout << "The int argument is " << "\"" << iQuote(x) << "\"" << endl;
    cout << "The double argument is "<< "\"" << iQuote(y) << "\"" << endl;
    cout << "the string argument is " << "\"" << iQuote(name) << "\"" << endl;
    return 0;
}
    
int iQuote(int a){      
    return(a);
}
    
double iQuote(double a){
    return (string(a));
}
    
string iQuote(string a){
    return(a);
}

如果函数 return 是 intdouble 等,则它不能 return 带有引号的值。引号仅适用于字符串。你将不得不做这样的事情:

#include <iostream>
#include <string>
    
using namespace std;
    
string iQuote(int);
string iQuote(double);
string iQuote(string);
        
int x = 4;
double y = 8.2;
string name = "Dan";
    
int main()
{
    cout << "The int argument is " << iQuote(x) << endl;
    cout << "The double argument is " << iQuote(y) << endl;
    cout << "the string argument is " << iQuote(name) << endl;
    return 0;
}

string iQuote(int a){
    return "\"" + to_string(a) + "\"";
    // or: return iQuote(to_string(a));
}

string iQuote(double a){
    return "\"" + to_string(a) + "\"";
    // or: return iQuote(to_string(a));
}

string iQuote(string a){
    return "\"" + a + "\"";
}

添加到@RemyLebeau 的回答中,如果你想要一种返回字符串的通用方法,这里有另一种方法:

template <typename T>
string iQuote(T a){
    return "\"" + to_string(a) + "\"";
}

std::string iQuote(std::string a){
    return "\"" + a + "\"";
}

只要传递给 iQuote 的类型与 std::to_string 兼容,您就不必为每个类型编写单独的函数。

#include <iostream>
#include <string>

template <typename T>
std::string iQuote(T a){
    return "\"" + std::to_string(a) + "\"";
}

std::string iQuote(std::string a){
    return "\"" + a + "\"";
}

int x = 4;
double y = 8.2;
std::string name = "Dan";
float f = 4.9f;

int main()
{
    std::cout << "The int argument is " << iQuote(x) << std::endl;
    std::cout << "The double argument is " << iQuote(y) << std::endl;
    std::cout << "the string argument is " << iQuote(name) << std::endl;
    std::cout << "the float argument is " << iQuote(f) << std::endl;
    return 0;
}

输出:

The int argument is "4"
The double argument is "8.200000"
the string argument is "Dan"
the float argument is "4.900000"

另一种通用方法是使用 std::ostringstream。那么只要类型有一个重载的operator <<,值就可以放在引号里:

#include <iostream>
#include <string>
#include <sstream>

template <typename T>
std::string iQuote(T a){
    std::ostringstream s;
    s << "\"" << a << "\"";
    return s.str();
}

int x = 4;
double y = 8.2;
std::string name = "Dan";
float f = 4.9f;

int main()
{
    std::cout << "The int argument is " << iQuote(x) << std::endl;
    std::cout << "The double argument is " << iQuote(y) << std::endl;
    std::cout << "the string argument is " << iQuote(name) << std::endl;
    std::cout << "the float argument is " << iQuote(f) << std::endl;
    return 0;
}

输出:

The int argument is "4"
The double argument is "8.2"
the string argument is "Dan"
the float argument is "4.9"

输出格式不同 -- 这是由于流的默认处理,而不是 to_string