简体   繁体   中英

How to assign to a 2d vector C++

I am lost on how to assign to a 2d vector like you would a 2d array. Array

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

With vectors I don't know if you need to use a pushback or what, but I need to be able to assign it to the Second spot inside the vector "[1][]" but I would prefer if I didn't have to resize the vector then assign to it if possible.

My Code

#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");
      }
   }
}

It seems like you should be using a map instead of a vector. Your key could be user and the associated value could be the password. This would avoid an O(N) loop through all users. If you're set on using vector, since your top level vector seems to only ever have two values, you could also use a single vector of structs (or pairs) each with the user and pass.

If you want to add a user and its password to the vector then the function will look like

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

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