Check if a specific index in a vector is "filled"

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Check if a specific index in a vector is "filled"

Post by Wanderer82 »

Hi

I have to know whether there is already a saved value at a specific index position of a vector that hasn't been initialized at its time of creation and doesn't have a fixed amount of elements. Is this even possible somehow? Something like:

Code: Select all

if(vector[index] == empty)
{
 //do something
}
I found similar questions on the internet but somehow all these answers don't work or are a bit different from what I need.

Thanks,
Thomas
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if a specific index in a vector is "filled"

Post by evstevemd »

I assume that vector is std::vector. If so check with empty()
an example copied verbatim from cpp dot com
https://cplusplus.com/reference/vector/vector/empty/

Code: Select all

// vector::empty
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int sum (0);

  for (int i=1;i<=10;i++) myvector.push_back(i);

  while (!myvector.empty())
  {
     sum += myvector.back();
     myvector.pop_back();
  }

  std::cout << "total: " << sum << '\n';

  return 0;
}
if you want to check vector Item at index is empty then it depends on type the vector is holding

Code: Select all

std::vector<wxString> names;
names.push_back("Mimi Hapa");
names.push_back("Yule Kule");
names.push_back("");

for(int i=0; i < names.size(); i++){
    if(name[i].IsEmpty()){
        wxPuts("Empty Name found...");
    }
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check if a specific index in a vector is "filled"

Post by Wanderer82 »

Hi and thanks for the tips.

I'm not sure if your second suggestion is what I need.If I create a vector of wxStrings and don't put any data in it (using eg. push.back), the indexes are "empty". Does that mean they hold an empty string like ""? If this is true, what about a vector with a class which is derived of wxPanels? What does an empty index hold then? It's exactly this what I want to know: Has there already been put an item into the vector at this index.

Thanks,
Thomas
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Check if a specific index in a vector is "filled"

Post by ONEEYEMAN »

Hi,
Are you pushing the "wxPanel *" or wxPanel?

In the former case you can check if the element exist by simply checking for nullptr.
In the latter - I don't think its possible.

But why do you want to know that?

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if a specific index in a vector is "filled"

Post by evstevemd »

Wanderer82 wrote: Fri Mar 24, 2023 5:49 pm.If I create a vector of wxStrings and don't put any data in it (using eg. push.back), the indexes are "empty"
It means the vector have got nothing. So it is empty. No index exist because index means something
Wanderer82 wrote: Fri Mar 24, 2023 5:49 pmIf this is true, what about a vector with a class which is derived of wxPanels? What does an empty index hold then?
Well if you don't put anything, then vector does not contain anything. No indices no nothing. It is empty. So it is not holding anything until you push something. You need to read a little bit about vectors and how they work.
Wanderer82 wrote: Fri Mar 24, 2023 5:49 pm It's exactly this what I want to know: Has there already been put an item into the vector at this index.
check for vector.size(), if is zero then there is nothing if it is 1 there is one item and so forth...!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Check if a specific index in a vector is "filled"

Post by ONEEYEMAN »

Hi,
Or just use std::vector.empty().

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Check if a specific index in a vector is "filled"

Post by doublemax »

Code: Select all

if(vector[index] == empty)
{
 //do something
}
First you need to check if the vector is big enough for that index. If the vector has a size 10, you can't access element any element with an index higher than 9. If you want to write into the vector at a certain index, that is beyond the vector's size, you need to resize() the vector first. That will not happen automatically. https://en.cppreference.com/w/cpp/conta ... tor/resize

If the vector contains pointers, empty entries will be NULL. So at the very least, your check should look like this:

Code: Select all

if( index < vector.size() && vector[index] == NULL)
{
 //do something
}
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check if a specific index in a vector is "filled"

Post by Wanderer82 »

Hi everyone

In the meantime I realized that actually I don't even need to check for this. But still all your advice might come in handy at some other point. I'm facing other "problems" but will open a new thread if needed.

Thanks so far,
Thomas
Post Reply