さまざまなタイプを含むがあり、 type=D,Sboost::variant
のようにする必要がある文字列があります。バリアントの値はそれぞれ D と S で、キーは「type」です。それは私が今その部分を繰り返しているところですmap<std::string, std::vector<variant> >
vector<variant>
ここで、最初に static_visitor をバリアントに適用して適切な変換を行います。この場合は必要ないかもしれませんが、他の型の場合は文字列への変換が必要になります。
ConcatValues
次に、この関数をヘルパー クラスの一部であると呼びます。vector<string> v_accumulator
この関数は while ループで数回呼び出される可能性があり、カンマ区切りの値のリストで終了したいため、 このクラスには一時的な結果を保持する定義があります。
v_accumulator
ただし、問題は、関数呼び出しごとにベクトルが常に空であることです。それがクラス変数であることを考えると、それはどのように意味がありますか。
while(variant_values_iterator != values.end())
{
variant var = *variant_values_iterator;
boost::apply_visitor( add_node_value_visitor( boost::bind(&SerializerHelper::ConcatValues, helper, _1, _2), key, result), var);
variant_values_iterator++;
}
std::string SerializerHelper::ConcatValues(std::string str, std::string key)
{
v_accumulator.push_back(str); //the previous value is not in this vector???
std::stringstream ss;
std::vector<std::string>::iterator it = v_accumulator.begin();
ss << key;
ss << "=";
for(;it != v_accumulator.end(); it++)
{
ss << *it;
if (*it == v_accumulator.back())
break;
ss << ",";
}
return ss.str();
}
class SerializerHelper
{
public:
std::string ConcatValues(std::string str, std::string key);
private:
std::vector<std::string> v_accumulator;
};
元のキーと値のペアの値の部分で D、S の値を連結する簡単な方法があるでしょうか?