use*_*679 28 c# c++ string string-formatting
我没有太多使用C++的经验.相反,我在C#中工作得更多,所以,我想问一下我在那里会做些什么.我必须生成一个特定的字符串格式,我必须传递给另一个函数.在C#中,我可以通过以下简单代码轻松生成字符串.
string a = "test";
string b = "text.txt";
string c = "text1.txt";
String.Format("{0} {1} > {2}", a, b, c);
Run Code Online (Sandbox Code Playgroud)
通过生成这样一个上面的字符串,我应该能够通过它system().但是,system只接受char*
我在Win32 C++(不是C++/CLI),并且不能使用,boost因为它包含太多包含项目本身非常小的所有文件.喜欢的东西,sprintf()在我看来是有用的,但sprintf不接受string的a,b和c参数.有什么建议我如何生成这些格式化的字符串以传递给我的程序中的系统?
dir*_*tly 35
C++方式是将std::stringstream对象用作:
std::stringstream fmt;
fmt << a << " " << b << " > " << c;
Run Code Online (Sandbox Code Playgroud)
C方式将是使用sprintf.
从那时起C方式难以理解:
当然,如果性能是一个问题,你可能想要回到C路上(想象一下你创造了固定大小的百万个小stringstream物体,然后将它们扔掉).
moo*_*eep 22
为了完整起见,您可以使用std::stringstream:
#include <iostream>
#include <sstream>
#include <string>
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";
}
Run Code Online (Sandbox Code Playgroud)
或者(在这种情况下)std::string自己的字符串连接功能:
#include <iostream>
#include <string>
int main() {
std::string a = "a", b = "b", c = "c";
std::string str = a + " " + b + " > " + c;
std::cout << str << "\n";
}
Run Code Online (Sandbox Code Playgroud)
以供参考:
如果你真的想要走C路.这个给你:
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
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<char>::size_type size = 256;
std::vector<char> 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";
}
Run Code Online (Sandbox Code Playgroud)
以供参考:
然后,有Boost格式库.为了你的例子:
#include <iostream>
#include <string>
#include <boost/format.hpp>
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";
}
Run Code Online (Sandbox Code Playgroud)
vit*_*aut 14
除了其他人建议的选项之外,我还可以推荐fmt库,它实现类似于str.formatPython和String.FormatC#中的字符串格式.这是一个例子:
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);
Run Code Online (Sandbox Code Playgroud)
免责声明:我是这个图书馆的作者.
你可以sprintf结合使用std::string.c_str().
c_str()返回a 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;
Run Code Online (Sandbox Code Playgroud)
或者,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() );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99480 次 |
| 最近记录: |