如何在C ++中使用指向多个向量的指针来指定字符串向量的向量?

时间:2011-08-20 20:08:29

标签: c++ string vector

我需要帮助找出一些代码。我不知道如何填充矢量>使用指向多个字符串向量的指针。

请仅在包含// HELP NEEDED HERED

的行中提出建议
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string>* pointerReturner (string str1, string str2, string str3)
{
    vector<string> *vList = new vector<string>();
    vList->push_back(str1); 
    vList->push_back(str2);
        vList->push_back(str3); 
    return vList;
}

int main(int argc, char* argv[]) {

    vector<string> *vMakeList1;
        vMakeList1 = pointerReturner ("1","8-20-2011","Ford");

        vector<string> *vMakeList2;
    vMakeList2 = pointerReturner ("2","8-20-2011","Honda");

        vector<string> *vMakeList3;
        vMakeList3 = pointerReturner ("3","8-20-2011","Toyota");

        vector<vector<string>> *MakeList;

        //HELP NEEDED HERE

delete vMakeList1, vMakeList2, vMakeList3;


    vector<vector<string> >::iterator i = MakeList->begin();
    for( ; i != MakeList->end(); ++i)
    {
        vector<string>::iterator pos = (*i).begin();
        for ( ; pos!=(*i).end(); ++pos)
            cout << *pos << endl;
    }

    cin.get();
    return 0;
}

结果是填充了相关数据的vector<vector<string>>

3 个答案:

答案 0 :(得分:1)

MakeList = new vector<vector<string> > ();

MakeList->push_back(*vMakeList1);
MakeList->push_back(*vMakeList2);
MakeList->push_back(*vMakeList3);

答案 1 :(得分:0)

你做不到。你需要一个vector<vector<string>*>* MakeList。并且,太多的指针和new蝙蝠侠。

答案 2 :(得分:0)

vector<vector<string>> *MakeList = new vector<vector<string>>();
MakeList->push_back(*vMakeList1);
MakeList->push_back(*vMakeList2);
MakeList->push_back(*vMakeList3);

似乎可以胜任。

这不是做事的方式,但你似乎已经意识到了这一点。