我们可以使用boost格式进行无序排序

问题描述 投票:0回答:1

让我们直接转到代码

#include <boost/format.hpp>
#include <iostream>
#include <vector>

using namespace std;

string strtemplate="First:%1%, Second:%2%";
int main()
{
    vector<string> v;
    v.push_back("first");
    v.push_back("second");
#if 0
    cout<<str(boost::format(strtemplate) % v[1] % v[0])<<endl;
#else
    boost::format fmter(strtemplate);
    for(int i=0;i<2;++i)
    {
            fmter % v[i];
    }
    cout<<str(fmter)<<endl;
#endif
    return 0;
}

预期的输出是内部的输出。 需要的是,我可能会不按顺序获取值,但希望它们放在我预先格式化的字符串中。 注意:我在这里使用了一个向量只是为了说明,所以遍历向量是逆向顺序不是我的选项。

c++ boost
1个回答
0
投票

例如,如果您的向量的顺序相反,为什么没有格式模板如下?

"First: %3%, Second %2%, Third: %1%.";

您需要事先了解输入向量与字符串中插槽顺序的关系,如果这是您要求的。否则,您需要对输入向量进行一些分析,并在运行时构建格式字符串,这会破坏目的。

我强烈建议您查看fmt库以进行字符串格式化,它比Boost格式更轻量级,而且速度更快。正在进行工作以使其达到标准。

© www.soinside.com 2019 - 2024. All rights reserved.