C ++中的String.Format替代

我没有太多使用C ++的经验。 相反,我在C#中工作得更多,所以,我想问一下我在那里会做些什么。 我必须生成一个特定的字符串格式,我必须将其传递给另一个函数。 在C#中,我可以通过以下简单代码轻松生成字符串。

string a = "test"; string b = "text.txt"; string c = "text1.txt"; String.Format("{0} {1} > {2}", a, b, c); 

通过生成这样一个上面的字符串,我应该能够在system()传递它。 但是, system只接受char*

我使用的是Win32 C++ (不是C ++ / CLI),并且不能使用boost因为它会包含太多包含项目本身非常小的所有文件。 像sprintf()这样的东西对我来说很有用,但是sprintf不接受string作为abc参数。 有什么建议我如何生成这些格式化的字符串以传递给我的程序中的系统?

您可以将sprintfstd::string.c_str()结合使用。

c_str()返回一个const char*并与sprintf

 string a = "test"; string b = "text.txt"; string c = "text1.txt"; char* x = new char[a.length() + b.length() + c.length() + 32]; sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() ); string str = x; delete[] x; 

或者,如果您知道大小,则可以使用预先分配的char数组:

 string a = "test"; string b = "text.txt"; string c = "text1.txt"; char x[256]; sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() ); 

C ++方式是使用std::stringstream对象:

 std::stringstream fmt; fmt << a << " " << b << " > " << c; 

C方式是使用sprintf

从那时起C方式难以理解:

  • 这种类型不安全
  • 需要缓冲管理

当然,如果性能是一个问题,你可能想要退回到C路上(想象一下你正在创建固定大小的数百万个小stringstream对象然后扔掉它们)。

为了完整起见,您可以使用std::stringstream

 #include  #include  #include  int main() { std::string a = "a", b = "b", c = "c"; // apply formatting std::stringstream s; s << a << " " << b << " > " << c; // assign to std::string std::string str = s.str(); std::cout << str << "\n"; } 

或者(在这种情况下) std::string的自己的字符串连接function:

 #include  #include  int main() { std::string a = "a", b = "b", c = "c"; std::string str = a + " " + b + " > " + c; std::cout << str << "\n"; } 

以供参考:


如果你真的想要走C路。 这个给你:

 #include  #include  #include  #include  int main() { std::string a = "a", b = "b", c = "c"; const char fmt[] = "%s %s > %s"; // use std::vector for memory management (to avoid memory leaks) std::vector::size_type size = 256; std::vector buf; do { // use snprintf instead of sprintf (to avoid buffer overflows) // snprintf returns the required size (without terminating null) // if buffer is too small initially: loop should run at most twice buf.resize(size+1); size = std::snprintf( &buf[0], buf.size(), fmt, a.c_str(), b.c_str(), c.c_str()); } while (size+1 > buf.size()); // assign to std::string std::string str = &buf[0]; std::cout << str << "\n"; } 

以供参考:


然后,有Boost格式库 。 为了你的例子:

 #include  #include  #include  int main() { std::string a = "a", b = "b", c = "c"; // apply format boost::format fmt = boost::format("%s %s > %s") % a % b % c; // assign to std::string std::string str = fmt.str(); std::cout << str << "\n"; } 

除了其他人建议的选项之外,我还可以推荐fmt库 ,它实现类似于Python中的str.format和C#中的String.Format字符串格式。 这是一个例子:

 std::string a = "test"; std::string b = "text.txt"; std::string c = "text1.txt"; std::string result = fmt::format("{0} {1} > {2}", a, b, c); 

免责声明:我是这个图书馆的作者。

为了完整起见,增强方式是使用boost::format

 cout << boost::format("%s %s > %s") % a % b % c; 

随便挑选。 增强解决方案具有sprintf格式的类型安全优势(对于那些发现<<语法有点笨拙的人)。

如前所述,C ++方式使用的是字符串流。

 #include  string a = "test"; string b = "text.txt"; string c = "text1.txt"; std::stringstream ostr; ostr << a << " " << b << " > " << c; 

请注意,您可以像这样从字符串流对象中获取C字符串。

 std::string formatted_string = ostr.str(); const char* c_str = formatted_string.c_str(); 

您可以只连接字符串并构建命令行。

 std::string command = a + ' ' + b + " > " + c; system(command.c_str()); 

您不需要任何额外的库。