find out if vector element exists

Nov 13, 2019 at 7:55pm
i am trying to use an element of a vector only if it exists (otherwise it will crash the program).
i have found how to check the whole vector to see if any information exists at all using vector::empty (although this is redundant as i could also use myVector.size), but i need to check an individual element. eg: if an element exists in myVector[10], then use it, else, do nothing.

is this possible?

thank you
Last edited on Nov 13, 2019 at 7:57pm
Nov 13, 2019 at 7:59pm
Looks like a job for std::find().
Nov 13, 2019 at 8:27pm
thanks for the help

my understanding (could be, and probably is wrong), from what i have read, is that std::find checks for a given value within the whole vector and tells you where it is stored if it finds it.
i am looking to find if the element itself exists at all.

e.g.
if i create a vector, then use push_back twice (giving me myVector[0] and myVector[1]), then use .erase to remove element 0. i want to be able to check myVector[0] to see if anything exists there (so the result should be false).

calling the element crashes the program, and i cant simply search for 0 using std::find because my vector is of type COORD.
also, i would assume an element that doesnt exist does not store a zero anyway?

i also do not know what the value of the element is before checking, so do not know what to std::find.

hope that makes more sense.

thanks for the help
Nov 13, 2019 at 8:33pm
std::find checks for a given value within the whole vector and tells you where it is stored if it finds it.
i am looking to find if the element itself exists at all.

std::find will return the iterator to "last" if no element is found. That's how you tell if the element doesn't exist.

https://en.cppreference.com/w/cpp/algorithm/find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> vec {3, 5, 8};
    
    if (std::find(vec.begin(), vec.end(), 4) != vec.end())
    {
        std::cout << "4 exists!\n";   
    }
    else
    {
        std::cout << "4 does not exist.\n";   
    }
}


________________

But I don't think this addresses your latest example. I don't think you're going about this the right way.

A Win32 API COORD object is just a struct with an X and a Y component, so you need to compare those values against expected X/Y values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
#include <vector>

struct MyCOORD {
    int x;
    int y;
};

bool operator==(const MyCOORD& c1, const MyCOORD& c2)
{
    return c1.x == c2.x && c1.y == c2.y;   
}

int main()
{
    std::vector<MyCOORD> vec { {2, 3}, {4, 42}, {5, 5} };
    
    if (std::find(vec.begin(), vec.end(), MyCOORD{2,3}) != vec.end())
    {
        std::cout << "MyCOORD{2,3} exists!\n";   
    }
    else
    {
        std::cout << "MyCOORD{2,3} does not exist.\n";   
    }
}
Last edited on Nov 13, 2019 at 8:42pm
Nov 13, 2019 at 8:42pm
oh ok i think that makes more sense! thanks!
so it is not searching for the value of 4, it is searching for the element #4 (like myVector[4])?

sorry if i am asking questions that the link answers, i dont understand a lot of the terminology at the moment.
Nov 13, 2019 at 8:43pm
No, it's searching for the value 4 in my fist example. I also added another example that uses a custom "MyCOORD" struct.
Nov 13, 2019 at 8:45pm
Example that removes an element from the vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <algorithm>
#include <vector>

struct MyCOORD {
    int x;
    int y;
};

bool operator==(const MyCOORD& c1, const MyCOORD& c2)
{
    return c1.x == c2.x && c1.y == c2.y;   
}

bool contains(const std::vector<MyCOORD>& vec, const MyCOORD& coord)
{
    return std::find(vec.begin(), vec.end(), MyCOORD{2,3}) != vec.end();
}

int main()
{
    std::vector<MyCOORD> vec { {2, 3}, {4, 42}, {5, 5} };
    
    if (contains(vec, MyCOORD{2,3}))
    {
        std::cout << "MyCOORD{2,3} exists!\n";   
    }
    else
    {
        std::cout << "MyCOORD{2,3} does not exist.\n";   
    }
    
    // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Erase-Remove
    vec.erase(std::remove(vec.begin(), vec.end(), MyCOORD{2,3}), vec.end());
    
        
    if (contains(vec, MyCOORD{2,3}))
    {
        std::cout << "MyCOORD{2,3} exists!\n";   
    }
    else
    {
        std::cout << "MyCOORD{2,3} does not exist.\n";   
    }
}

MyCOORD{2,3} exists!
MyCOORD{2,3} does not exist.
Last edited on Nov 13, 2019 at 8:47pm
Nov 13, 2019 at 9:00pm
if i create a vector, then use push_back twice (giving me myVector[0] and myVector[1]), then use .erase to remove element 0. i want to be able to check myVector[0] to see if anything exists there (so the result should be false).

It does not work that way.
1
2
3
4
5
6
std::vector<int> myV; // empty vector, size() == 0

myV.push_back(7);  // size() == 1, myV[0] == 7
myV.push_back(42); // size() == 2, myV[0] == 7, myV[1] == 42

myV.erase(myV.begin()); // size() == 1, myV[0] == 42 

See? The indexing can change when you insert/erase elements.

eg: if an element exists in myVector[10], then use it, else, do nothing.
1
2
3
if ( k < myVector.size() ) {
  // use myVector[k]
}


If you refer to (MyCOORD) values with persistent indices and some indices do not always have a value, then use std::map.
1
2
3
4
5
6
7
std::map<int,MyCOORD> mymap;
mymap[6] = MyCOORD{2,3};

auto it = mymap.find( 6 );
if (it != mymap.end() ) {
  // use it->second
}
Last edited on Nov 13, 2019 at 9:00pm
Nov 13, 2019 at 9:06pm
so i have to use the COORD values? there is no way to use the element number at all?

my program is a basic command line game with a 2D grid and bullets. i have used COORD vectors to give the positions of each bullet. i am destroying a bullet when it hits a wall using .erase and creating new bullets using .push_back.

if i have to use the COORDs then i could probably use a combination of the COORD and the direction of travel of the the bullet to trigger .erase, but it seems like a lot of work compared to using the element number.

i have probably approached the whole thing wrong though, i am very new to C++ and am still learning core things while playing around. the game is probably more than i can do, but i am learning a lot from doing it.

EDIT:
just read the updates and i see why i cant use the element number. thanks for the info
Last edited on Nov 13, 2019 at 9:09pm
Topic archived. No new replies allowed.