2017-09-17 124 views
0

我的目標是創建TeamV類型的「TeamS」向量。 TeamV應該有4個部分的數據。例如:TeamV [0] = teamID,member1,member2,member3。 我找到了一種獲取ID數組並分發到TeamV向量的方法。但我無法同時分發3名團隊成員。我該怎麼辦?從二維數組轉換爲矢量

#include <iostream> 
#include <vector> 

using namespace std; 
const int NUM_TEAMS = 4; 
const int NUM_MEMBERS = 3; 

struct TeamS{ 
    int ID; 
    string teamMembers[3]; 
}; 


void Initialize (vector <TeamS> & TeamV, const int id[], 
      const string m[][NUM_MEMBERS], int arraySize){ 
for (int i = 0; i<NUM_TEAMS;i++){ 
    for (int x=0; x<NUM_TEAMS;x++){ 
     for (int y = 0; y<NUM_MEMBERS;y++){ 
      TeamV.push_back({id[i],m[x][y]}); 
     } 
     } 
    } 
} 


int main(){ 

vector <TeamS> TeamV; //content of arrays below should go in to this vector 

const int ID [NUM_TEAMS] ={ 123, 321, 456, 789}; 
const string MEMBERS [NUM_TEAMS] [NUM_MEMBERS ] = 
{ 
    {"Sarah", "Joe", "John"}, 
    {"Chris", "Kevin", "James"}, 
    {"Tom", "Kim", "Emily"}, 
    {"Jill", "Jason", "Jim"} 
}; 

Initialize(TeamV, ID, MEMBERS, NUM_TEAMS*NUM_MEMBERS); 
} 
+0

你應該做的是初始化數組成員爲'{a,b,c}'。 –

+0

贊[this](https://ideone.com/q4YsvQ)?或者你是否想要複製任意數組? – Drop

+0

我想你在這裏犯了一個錯誤:'TeamV.push_back({id [i],m [x] [y]})'。儘管TeamS結構需要3名團隊成員,但您可以push_back ID和1個團隊成員(x和y位置)。所以當他傳遞TeamS結構時,@Drop的解決方案很好。 –

回答

0

這就是Inistialize的樣子。

void Initialize (vector <TeamS> & TeamV, const int id[], 
       const string m[][NUM_MEMBERS], int arraySize){ 
    TeamV.resize(NUM_TEAMS); 
    for (int i = 0; i<NUM_TEAMS;++i) { 
    TeamV[i].ID = id[i]; 
    for (int y = 0; y<NUM_MEMBERS;++y){ 
     TeamV[i].teamMembers[y] = m[i][y]; 
    } 
    } 
} 

但你有沒有考慮過,你真的應該看看​​而不是?