简体   繁体   English

字符串向量初始化的向量

[英]vector of string vector initialization

I wonder how could I simplify and generalize this initialization of a vector of vector of string : 我想知道如何简化和推广string向量的向量的初始化:

vector<vector<wstring>> vvValues;

const wchar_t *row1[] = { L"R1C1_variablesize"};
const wchar_t *row2[] = { L"R2C1_vsize" , L"R2C2_varsize", L"R2C3_variabsize"};
const wchar_t *row3[] = { L"R3C1_variablsize", L"R3C2_vasize"};

vvValues.push_back (vector<wstring> (row1, end(vrow1)));
vvValues.push_back (vector<wstring> (row2, end(row2)));
vvValues.push_back (vector<wstring> (row3, end(row3)));

I try to use temporary array of array 我尝试使用数组的临时数组

const wchar_t **rows[] = {row1, row2, row3);

Using iterator, I successfully test 使用迭代器,我成功测试

for (auto it = begin(rows); it!= end(rows); ++it)
    vvValues.push_back (vector<std::wstring> (*it, *it + 0));

Using count(), sizeof() or end() on row1, row2, row3, rows works as expected. 在row1,row2,row3,rows上使用count(),sizeof()或end()可以按预期工作。

-> BUT I can't figure out how to get count of elements on each row -> 但我不知道如何获取每一行的元素数

ie

sizeof(rows[0]) -> 4
sizeof(rows[1]) -> 12
sizeof(rows[2]) -> 8

or even better 甚至更好

sizeof(it) -> 4, 12, 8 on each iteration.

MANY THANKS 非常感谢

You lead me to this Working solution 您将我引向此工作解决方案

vector<vector<wstring>> vvValues;

// First string -> # of strings excluding index
const wchar_t *row1[] = { L"1", L"R1C1_variablesize"};
const wchar_t *row2[] = { L"3", L"R2C1_vsize" , L"R2C2_varsize", L"R2C3_variabsize"};
const wchar_t *row3[] = { L"2", L"R3C1_variablsize", L"R3C2_vasize"};

const wchar_t **rows[] = {row1, row2, row3);

for (unsigned long it = 0; it < sizeof(rows)/sizeof(wchar_t *); ++it)
    vvValues.push_back (vector<std::wstring> (rows[it] + 1, 
                                       rows[it] + 1 + wcstoul(rows[it][0], NULL, 0)));

Did you think about ending each row with a null string? 您是否考虑过以空字符串结尾每一行? For example: 例如:

const wchar_t **rows[] = {"Hello", "everyone", "", 
                          "start", "of", "row", "2", "", 
                          "now", "three", ""};

You can easily check for nulls in your initialization loop. 您可以在初始化循环中轻松检查是否为空。

If your strings can be null, however, you can try using a strange string that is rarely used as a marker. 但是,如果字符串可以为null,则可以尝试使用很少用作标记的奇怪字符串。 Say "~~12~" or even better use a unicode character not part of your language. "~~12~" ,甚至最好使用不属于您语言的unicode字符。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM