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.
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:
// 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
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.
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?
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:
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.