Wolfhrat
Wolfhrat

Reputation: 11

Check if a vector contains object with already entered values

so I'm struggling with these things:

I have method that returns istream input and takes istream input as a parameter, sends values to vector and stores them in it. Now, when I've entered 1 value, I'm trying to make a check if vector already contains that value, here is my code to understand it better:

struct Predmet {
    string naziv;
    string odsjek;
    istream& dodaj_predmet(istream &);
};

struct Student {
    string brojIndeksa;
    string ime;
    string prezime;
    map<std::string, int> ocjene;
    istream& dodaj_studenta(istream &);
};

vector<Student> studenti;
vector<Predmet> predmeti;
Student s;
Predmet p;

istream& Student::dodaj_studenta(istream & input){
    cout << "### Unesite podatke o studentu: " << endl;
    cout << endl;
    cout << "Unesite broj indeksa studenta: " << endl;
    getline(input, brojIndeksa);
    cout << endl;
    cout << "Unesite ime studenta: " << endl;
    getline(input, ime);
    cout << endl;
    cout << "Unesite prezime studenta: " << endl;
    getline(input, prezime);
    cout << endl;
    cout << "***     Uspjesno ste unijeli studenta     ***" << endl;
    return input;
}

So I'm trying to make a check if value entered for structure member brojIndeksa already exists in vector, and I'm not sure how to do it.

Also how can I add values to map, because I need to add pair<string, int>, and not sure how, because I need to check if STRING is a value entered in naziv inside predmeti vector?

Thanks.

Upvotes: 0

Views: 1061

Answers (2)

sucksatnetworking
sucksatnetworking

Reputation: 92

For your first question regard checking for a Student: you can add this search after the input.

    auto clean_buf = input.rdbuf()
    ....
    auto resit = std::find_if(v.begin(), v.end(), 
                 [&](auto& student) { 
                   return ( student.brojIndeska == brojIndeska)
                 });
    if(resit != v.end()) {//student exists
      input.rdbuf(clean_buf);
      return input;
     }

For the second, you can add to a map using

map.emplace(var1, var2)

Upvotes: 1

Dusan Todorovic
Dusan Todorovic

Reputation: 480

first of all, you can check count of std::vector to see if given key exists

//std::count(v.begin(), v.end(), key)
if (std::count(v.begin(), v.end(), key)){
    //it is inside
}
else{
    //it isn't inside
}

This is one way to go, but you should do the map

std::map<std::string,int> myMap;
//adding will look like this
std::pair<std::string,int> element;
myMap.insert(element);
//or
myMap.insert(std::pair<std::string,int>(myString,myInt));

Will solve your problem. You add pair of string and int. In order to check if given key exists you can do

std::map<std::string, int> m;
if ( m.find(key) == m.end() ) {
  // not found
} else {
  // found
}

Added Kyle comment to, as it is valid way, and it looks a bit cleaner, you can also find if element exists like this

if (m.count(key)){
    //key found
}
else{
    //key not found
}

PS. It's bad practice to use c++ using namespace std;. You should always use std::whatever_you_need instead

Upvotes: 1

Related Questions