2015-04-05 52 views
0

我迷失在如何分配給2d向量,就像你會2d數組。 陣列如何分配給2d向量C++

MyArray[0][1] = "User"; 
MyArray[1][1] = "Pass"; 

有了載體,我不知道,如果你需要使用反推或什麼,但我需要能夠把它分配給了向量內部的第二點「[1] []」,但我寧願如果我不必調整矢量大小然後分配給它,如果可能的話。

我的代碼

#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <stdlib.h> 

using namespace std; 

vector<vector <string> > UserPass(2,vector <string> (1)); 
string User, Pass; 
string lower(string var); 
string resetInput; 
string input; 
int x; 
int y; 
void initUsers(); 
void prompt(); 
void password(); 
void login(); 
bool loginSuccess; 


int main() 
{ 
    initUsers(); 
    prompt(); 
    return 0; 
} 

void initUsers() { 
    UserPass[0][0] = "admin"; 
    UserPass[1][0] = "admin"; 
} 

void addUser(string User, string Pass) { 
\\This is where I would add onto the vector 
} 

string lower(string var) { 
    transform(var.begin(),var.end(),var.begin(), ::tolower); 
    return var; 
} 

void login() 
{ 
    cout << "Please Enter your Username\n"; 
    cin >> User; 

    lower(User); 
    cout << "Please Enter your Password\n"; 
    cin >> Pass; 

    for (unsigned x = 0; x < 1; x++) { 
     for (unsigned y = 0; y < UserPass.size(); y++) { 
      if ((User == UserPass[x][y]) && (Pass == UserPass[x+1][y])) { 
       loginSuccess = true; 
       cout << "You have successfully logged in to your account." << endl; 
       break; 
      } 
     } 
    } 
    if (loginSuccess == false) { 
      cout << "Incorrect password or username.\n"; 
    } 
} 

void password() 
{ 
    while(true) { 
     cout << "Reset your Password to: \n"; 
     getline(cin, resetInput); 

     UserPass[x+1][y] = resetInput; 
     cout << UserPass[x+1][y] << " is now your new password\n"; 
     break; 
    } 
} 

void prompt() 
{ 
    while(true){ 
     cout << ">"; 
     getline(cin,input); 

     lower(input); 
     if (input == "login" && loginSuccess == true) { 
      cout << "You are already logged in as <" << User << ">" << endl; 
     } 
     if (input == "login" && loginSuccess == false) { 
      login(); 
     } 
     if ((input == "logout" && loginSuccess == false) or (input == "password" && loginSuccess == false)) { 
      cout << "You have not logged in yet.\n"; 
     } 
     if (input == "logout" && loginSuccess == true) { 
      loginSuccess = false; 
      cout << "You have successfully logged out.\n"; 
     } 
     if (input == "password" && loginSuccess == true) { 
      password(); 
     } 
     if (input == "exit") { 
      exit(EXIT_SUCCESS); 
     } 
     if (input == "clear"){ 
      system("cls"); 
     } 
    } 
} 

回答

1

好像你應該使用地圖,而不是一個向量。您的密鑰可能是用戶,相關聯的值可能是密碼。這將避免通過所有用戶的O(N)循環。如果你使用矢量,因爲你的頂級矢量似乎只有兩個值,你也可以使用一個單獨的結構向量(或對),每個向量都與用戶和通過。

0

如果你想添加一個用戶和密碼的載體則函數看起來像

void addUser(const std::string &User, const std::string &Pass) 
{ 
    UserPass.push_back({ User, Pass }); 
} 
+0

爲什麼和用戶或通行證 – chbchb55 2015-04-05 22:14:27

+0

@ chbchb55當參數聲明如下參考文獻,然後參數不被複制和創建調用它們的拷貝構造函數,然後調用析構函數。 – 2015-04-06 04:58:02

+0

當我嘗試使用類似於 cout << UserPass [0] [1]的方式訪問矢量時,我的程序崩潰了; – chbchb55 2015-04-06 22:34:57