c++ 中的 sprintf?

发布于 2024-11-06 06:29:24 字数 193 浏览 0 评论 0 原文

我正在寻找 C++ 中的 sprintf 。

我想构建一个 mysql 查询字符串,但如果我这样做(max_limit 是一个 const int),

std::string query = "select * from bla limit " + max_limit;

查询将无法工作。

I'm searching for an sprintf in c++.

I want to build a mysql query string but if I do it like (max_limit is an const int)

std::string query = "select * from bla limit " + max_limit;

The query wont work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜中书 2024-11-13 06:29:24

在 C++11 中,这变得太容易了。使用 std::to_string()如:

std::string query = "select * from bla limit " + std::to_string(max_limit);

完成!


旧解决方案,适用于仍在使用 C++03 的用户。

使用 stringbuilder 并动态创建 std::string 如下:

std::string query = stringbuilder() << "select * from bla limit " << max_limit;

其中 stringbuilder 实现为:

struct stringbuilder
{
   std::stringstream ss;
   template<typename T>
   stringbuilder & operator << (const T &data)
   {
        ss << data;
        return *this;
   }
   operator std::string() { return ss.str(); }
};

您可以使用 stringbuilder > 以多种不同的方式,例如:

std::string g(int m, int n) 
{
    //create string on the fly and returns it
    if ( m < n )
        return stringbuilder() << m << " is less than " << n ;
    return stringbuilder() << n << " is less than " << m ;
}

void f(const std::string & s );

//call f while creating string on the fly and passing it to the function
f(stringbuilder() << '{' << pc << '}' ); //passed as std::string

//this is my most favorite line
std::string s = stringbuilder() << 23  << " is greater than " << 5 ;

参见 ideone 的演示: http://ideone.com/J995r

以及看看我的关于此的博客:仅在一行中动态创建字符串

In C++11, this has been made too easy. Use std::to_string() as:

std::string query = "select * from bla limit " + std::to_string(max_limit);

Done!


OLD SOLUTION, for those who still use C++03.

Use stringbuilder and create std::string on the fly as:

std::string query = stringbuilder() << "select * from bla limit " << max_limit;

where stringbuilder is implemented as:

struct stringbuilder
{
   std::stringstream ss;
   template<typename T>
   stringbuilder & operator << (const T &data)
   {
        ss << data;
        return *this;
   }
   operator std::string() { return ss.str(); }
};

You can use stringbuilder in many different ways, such as:

std::string g(int m, int n) 
{
    //create string on the fly and returns it
    if ( m < n )
        return stringbuilder() << m << " is less than " << n ;
    return stringbuilder() << n << " is less than " << m ;
}

void f(const std::string & s );

//call f while creating string on the fly and passing it to the function
f(stringbuilder() << '{' << pc << '}' ); //passed as std::string

//this is my most favorite line
std::string s = stringbuilder() << 23  << " is greater than " << 5 ;

See demo at ideone : http://ideone.com/J995r

And see my blog on this : Create string on the fly just in one line

梦中的蝴蝶 2024-11-13 06:29:24

您不需要 sprintf,它不适用于字符串。像这样的事情:

#include <sstream>
#include <string>

template <typename T>
std::string Str( const T & t ) {
    std::ostringstream os;
    os << t;
    return os.str();
}

会完成这项工作。然后你可以说:

std::string query = "select * from bla limit " + Str( max_limit );

You don't want sprintf, it doesn't work with strings. Something like this:

#include <sstream>
#include <string>

template <typename T>
std::string Str( const T & t ) {
    std::ostringstream os;
    os << t;
    return os.str();
}

will do the job. You can then say:

std::string query = "select * from bla limit " + Str( max_limit );
水中月 2024-11-13 06:29:24

也许您想看看 boost::format 库。
它提供了 sprintf 的语法以及 c++ 的便利。
所以你的例子是:

std::string str = (boost::format("select * from bla limit %d") % max_limit).str();

Maybe you want to take a look at the boost::format lib.
It provides the syntax of sprintf with the convenience of c++.
So your example would be:

std::string str = (boost::format("select * from bla limit %d") % max_limit).str();
温柔一刀 2024-11-13 06:29:24

或者只使用宏?
#define QueryString(msg) ((static_cast(std::ostringstream().seekp(0, std::ios_base::cur)<

用法:
std::string query = QueryString("select * from mytable where x="<<30);

Or just use a macro?
#define QueryString(msg) ((static_cast<std::ostringstream&>(std::ostringstream().seekp(0, std::ios_base::cur)<<msg)).str())

Usage:
std::string query = QueryString("select * from mytable where x="<<30);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文