C++ 在C+中引用字符串+;

C++ 在C+中引用字符串+;,c++,string,double-quotes,C++,String,Double Quotes,在Pascal Lazarus/Delphi中,我们有一个函数QuotedStr(),它将任何字符串包装在单引号内 这是我当前C++代码的一个例子: //I need to quote tblCustomers pqxx::result r = txn.exec( "Select * from \"tblCustomers\" "); 另一个: //I need to quote cCustomerName std::cout << "Name: " << r[a][

在Pascal Lazarus/Delphi中,我们有一个函数QuotedStr(),它将任何字符串包装在单引号内

这是我当前C++代码的一个例子:

//I need to quote tblCustomers
pqxx::result r = txn.exec( "Select * from \"tblCustomers\" "); 
另一个:

//I need to quote cCustomerName
std::cout << "Name: " << r[a]["\"cCustomerName\""];
//我需要引用cCustomerName
标准::cout
查看更多选项


查看更多选项

无标准函数,除非您计算std::basic_string::operator+()
,但编写它很简单


我有点困惑是什么让你慢下来了-
引用(“cCustomerName”)
是更多的字符,不是吗?:>

没有标准函数,除非您计算
std::basic_string::operator+()
,但编写它很简单


我有点困惑是什么让你慢下来了-
引用(“cCustomerName”)
是更多的字符,不是吗?:>

您可以使用自己的占位符字符代表引号,一些永远不会使用的ASCII符号,并将其替换为“在输出字符串之前”。您可以使用自己的占位符字符代表引号,一些永远不会使用的ASCII符号,并将其替换为”在输出字符串之前。

使用C++11可以创建如下用户定义的文本:

#include <iostream>
#include <string>
#include <cstddef>

// Define user defined literal "_quoted" operator.
std::string operator"" _quoted(const char* text, std::size_t len) {
    return "\"" + std::string(text, len) + "\"";
}

int main() {
    std::cout << "tblCustomers"_quoted << std::endl;
    std::cout << "cCustomerName"_quoted << std::endl;
}
如果需要,您甚至可以使用较短的名称定义运算符,例如:

std::string operator"" _q(const char* text, std::size_t len) { /* ... */ }
// ...
std::cout << "tblCustomers"_q << std::endl;
std::string操作符“”\u q(const char*text,std::size\u t len){/*…*/}
// ...

std::cout使用C++11可以创建如下用户定义的文本:

#include <iostream>
#include <string>
#include <cstddef>

// Define user defined literal "_quoted" operator.
std::string operator"" _quoted(const char* text, std::size_t len) {
    return "\"" + std::string(text, len) + "\"";
}

int main() {
    std::cout << "tblCustomers"_quoted << std::endl;
    std::cout << "cCustomerName"_quoted << std::endl;
}
如果需要,您甚至可以使用较短的名称定义运算符,例如:

std::string operator"" _q(const char* text, std::size_t len) { /* ... */ }
// ...
std::cout << "tblCustomers"_q << std::endl;
std::string操作符“”\u q(const char*text,std::size\u t len){/*…*/}
// ...
std::cout
#包括
#包括
结构引用
{
常量字符*_文本;
quoted(常量字符*文本):_text(文本){}
运算符std::string()常量
{
std::string quotedStr=“\”;
quotedStr+=\u文本;
quotedStr+=“\”;
返回quotedStr;
}
};
std::ostream和操作员
#包括
#包括
结构引用
{
常量字符*_文本;
quoted(常量字符*文本):_text(文本){}
运算符std::string()常量
{
std::string quotedStr=“\”;
quotedStr+=\u文本;
quotedStr+=“\”;
返回quotedStr;
}
};

std::ostream&operatorC++14添加了
std::quoted
,它正是这样做的,更实际的是:它负责在输出流中转义引号和反斜杠,并在输入流中取消转义引号和反斜杠。它是有效的,因为它不创建新字符串,它实际上是一个IO操纵器。(因此,您不会像您希望的那样获得字符串。)


如中所述,它实际上是为字符串的往返而设计的。

std::quoted
,它正是这样做的,更实际的是:它负责在输出流中转义引号和反斜杠,并在输入流中取消转义引号和反斜杠。它是有效的,因为它不创建新字符串,它实际上是一个IO操纵器。(因此,您不会像您希望的那样获得字符串。)


如中所述,它实际上是为字符串的往返而设计的。

使用一些C函数和反斜杠来转义引号怎么样? 像sprintf_s:

#define BUF_SIZE 100
void execute_prog() {

  //Strings that will be predicted by quotes 
  string param1 = "C:\\users\\foo\\input file.txt", string param2 = "output.txt";

  //Char array with any buffer size 
  char command[BUF_SIZE];

  //Concating my prog call in the C string.
  //sprintf_s requires a buffer size for security reasons
  sprintf_s(command, BUF_SIZE, "program.exe  \"%s\" \"%s\"", param1.c_str(), 
    param2.c_str());

  system(command);

}
结果字符串为:

program.exe "C:\users\foo\input file.txt" "output.txt"

下面是.

使用一些C函数和反斜杠来转义引号怎么样? 像sprintf_s:

#define BUF_SIZE 100
void execute_prog() {

  //Strings that will be predicted by quotes 
  string param1 = "C:\\users\\foo\\input file.txt", string param2 = "output.txt";

  //Char array with any buffer size 
  char command[BUF_SIZE];

  //Concating my prog call in the C string.
  //sprintf_s requires a buffer size for security reasons
  sprintf_s(command, BUF_SIZE, "program.exe  \"%s\" \"%s\"", param1.c_str(), 
    param2.c_str());

  system(command);

}
结果字符串为:

program.exe "C:\users\foo\input file.txt" "output.txt"


以下是.

对于C++11,您可以使用原始字符串文本:例如,看,这似乎需要更多的工作。我正在寻找加快工作速度的方法:)谢谢:)
R“(…)”
“…”
还有更多的工作要做,还要担心到处逃走吗?不知道你是怎么想的。如果只有一两个字符需要转义,那么可能会更加冗长,但不需要太多思考或出错……对于C++11,您可以使用原始字符串文本:例如,请参阅,这似乎需要更多的工作。我正在寻找加快工作速度的方法:)谢谢:)
R“(…)”
“…”
还有更多的工作要做,还要担心到处逃走吗?不知道你是怎么想的。如果只有一个或两个字符需要转义,那么可能会更加冗长,但不需要太多考虑或出错……谢谢,但对于双引号字符串来说,这似乎是同等的工作量。有没有标准函数可以执行此操作?
String QuotedStr(String"es,String&orig){return quotes+orig+quotes;}
我只想传递字符串,因为我总是想对它进行双引号引用。谢谢,但这对双引号字符串来说似乎是同等工作量。有没有标准的函数来实现这一点?
String QuotedStr(String"es,String&orig){return quotes+orig+quotes;}
我只想传递字符串,因为我总是想对它进行双引号。是的,它看起来像是更多的字符。但当我必须经常这样做时,键入转义引号会更快,更不容易出错。但当我不得不经常这样做时,键入转义引号会更快,更不容易出错。谢谢。ASCII符号是否兼容OSs和英语、阿拉伯语和德语等语言?我想你的意思是Unicode:)ASCII不是Unicode。谷歌搜索ASCII表。与外部世界的兼容性并不重要,这只是在源代码中。在输出字符串之前替换它。谢谢。ASCII符号是否兼容OSs和英语、阿拉伯语和德语等语言?我想你的意思是Unicode:)ASCII不是Unicode。谷歌搜索ASCII表。与外部世界的兼容性并不重要,这只是在源代码中。在输出字符串之前替换它。不要在字符串开头加下划线things@doctorlove
17.6.4.3.5-不以下划线开头的文字标识符保留供将来标准化使用。
不要在字符开头加下划线things@doctorlove
17.6.4.3.5-不包含以下内容的文字支持标识符以下划线开头,保留用于将来的标准化。
program.exe "C:\users\foo\input file.txt" "output.txt"