如何将多个 QStringLists 中的项目添加到一个?
How to add items from multiple QStringLists to one?
如果我有几个QStringLists,例如:
QStringList str1 = {"name1", "name2", "name3"};
QStringList str2 = {"value1", "value2", "value3"};
QStringList str3 = {"add1", "add2", "add3"};
有没有什么方法可以获取像 QStringList listAll;
这样的列表列表(嵌套列表),它看起来像这样:
(("name1","value1","add1"),("name2","value2","add2"),("name3","value3","add3"))
从评论来看,您似乎正在尝试将字符串列表打包为 QVector
而不是 QList
。在这种情况下,只需遍历 QStringList
并将从相等索引创建的字符串列表附加到向量。
#include <QVector>
QVector<QStringList> allList;
allList.reserve(str1.size()); // reserve the memory
for(int idx{0}; idx < str1.size(); ++idx)
{
allList.append({str1[idx], str2[idx], str3[idx]});
}
你不需要向量,继续使用 StringList
QStringList str1={"name1", "name2", "name3"};
QStringList str2={"value1", "value2", "value3"};
QStringList str3={"add1", "add2", "add3"};
QStringList r{};
// use elegantly the << operator and the join method instead...
r << str1.join(",") << str2.join(",") << str3.join(",");
qDebug() << "result : " << r.join(";");
//result:
//"name1,name2,name3;value1,value2,value3;add1,add2,add3"
如果我有几个QStringLists,例如:
QStringList str1 = {"name1", "name2", "name3"};
QStringList str2 = {"value1", "value2", "value3"};
QStringList str3 = {"add1", "add2", "add3"};
有没有什么方法可以获取像 QStringList listAll;
这样的列表列表(嵌套列表),它看起来像这样:
(("name1","value1","add1"),("name2","value2","add2"),("name3","value3","add3"))
从评论来看,您似乎正在尝试将字符串列表打包为 QVector
而不是 QList
。在这种情况下,只需遍历 QStringList
并将从相等索引创建的字符串列表附加到向量。
#include <QVector>
QVector<QStringList> allList;
allList.reserve(str1.size()); // reserve the memory
for(int idx{0}; idx < str1.size(); ++idx)
{
allList.append({str1[idx], str2[idx], str3[idx]});
}
你不需要向量,继续使用 StringList
QStringList str1={"name1", "name2", "name3"};
QStringList str2={"value1", "value2", "value3"};
QStringList str3={"add1", "add2", "add3"};
QStringList r{};
// use elegantly the << operator and the join method instead...
r << str1.join(",") << str2.join(",") << str3.join(",");
qDebug() << "result : " << r.join(";");
//result:
//"name1,name2,name3;value1,value2,value3;add1,add2,add3"