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
作为a
, b
和c
参数。 有什么建议我如何生成这些格式化的字符串以传递给我的程序中的系统?
您可以将sprintf
与std::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());
您不需要任何额外的库。