简体   繁体   中英

vector of string vector initialization

I wonder how could I simplify and generalize this initialization of a vector of vector of 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.

-> 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. Say "~~12~" or even better use a unicode character not part of your language.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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