check if a value is already present in vector
-
Hello,
i'm trying create a database of name & email's.
but i end up with 2049 records in the vector according to QT Creator debug, instead of the 30 delivered.vector< pair <string, string> > data; for(........){ string firstName = ...........; // automated data string emailID = ................;// automated data if(data.begin() == data.end()){ //if(data.empty() == 0){ std::cout << "add first record" << std::endl; data.push_back( make_pair(firstName,emailID) ); } else{ for(auto ii : data){ std::cout << "ii: " << ii.first << std::endl; if((ii.first == firstName)&&((ii.second == emailID))){ std::cout << "already in database" << std::endl; } else{ data.push_back( make_pair(firstName,emailID) ); } } } }
what am i doing wrong?
everything looks ok, if i view the data in QT Creator where it holds due to a segmentation fault.if((ii.first == firstName)&&((ii.second == emailID))){
compare values are valid where it stals.
accept having way more records in the vector than expected.this is the data:
riya riya@gmail.com
julia julia@julia.me
julia sjulia@gmail.com
julia julia@gmail.com
samantha samantha@gmail.com
tanya tanya@gmail.com
riya ariya@gmail.com
julia bjulia@julia.me
julia csjulia@gmail.com
julia djulia@gmail.com
samantha esamantha@gmail.com
tanya ftanya@gmail.com
riya riya@live.com
julia julia@live.com
julia sjulia@live.com
julia julia@live.com
samantha samantha@live.com
tanya tanya@live.com
riya ariya@live.com
julia bjulia@live.com
julia csjulia@live.com
julia djulia@live.com
samantha esamantha@live.com
tanya ftanya@live.com
riya gmail@riya.com
priya priya@gmail.com
preeti preeti@gmail.com
alice alice@alicegmail.com
alice alice@gmail.com
alice gmail.alice@gmail.com"some" people have 2 email addresses
-
C++ basics - you modify the vector while iterating over it - this can't work.
-
@Natural_Bugger
Additional to @Christian-Ehrlicher: even if your code did not crash the iteration, it would add a new pair every time an existing pair did not match. This can hardly be what is intended, can it? -
what would be the approach then?
if i can't iterate, but yet have to check if the value exists. -
@Natural_Bugger said in check if a value is already present in vector:
what would be the approach then?
use a new vector, iterator only over the initial size, ...
-
@Natural_Bugger
Of course you can iterate. But you cannot modify the list by adding while you do so. Clearly you need to iterate to check if something is present, and only add after the iteration if it was not. -
template<typename T1, typename T2> bool contains(T1 container, T2 value){ auto result = std::find(container.begin(), container.end(), value); return result != std::end(container); } int main(int argc, char *argv[]) { QApplication a(argc, argv); std::vector<int> v {1,2,3,4,5,6,7}; for(int i (0); i < 10; i++) { if(!contains(v,i)) v.push_back(i); } qDebug() << v; // return a.exec(); }
-
@J-Hilk : I would change it to
std::vector<int> v {1,2,3,4,5,6,7}; const auto initialSize = v.size(); for (int i = 0; i < initialSize; i++) { if (!contains(v, i)) v.push_back(i); }
-
@Christian-Ehrlicher @J-Hilk @JonB
thank you all.
this is how i solved it, i just had to adapt it to pair.
if (std::find(data.begin(), data.end(), std::pair<std::string, string>(firstName, emailID)) != data.end()) std::cout << "Element found"; else{ std::cout << "Element not found"; data.push_back( make_pair(firstName,emailID) ); }