Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i have doubt regarding GetUPPerBound whats the difference between GetUPPerBound and GetSize how to do do in vector c++ getupperbound
Posted
Comments
[no name] 9-Nov-15 1:39am    
Have you read the documentation? "Because indexes are zero-based, the size is 1 greater than the largest index". What don't you understand about that statement?
[no name] 9-Nov-15 1:42am    
i got it but i want how to do in the vector
[no name] 9-Nov-15 1:43am    
can i do with size - 1

1 solution

CArray::GetUpperBound() is used to get the UpperBound of the array(last index of the array).
Because array indexes are zero-based, this function returns a value 1 less than GetSize.
GetUpperBound( ) give –1 when array contains no elements.
C++
CArray<cpoint,cpoint> myArray;
CPoint pt;
for (int i = 0; i < 10; i++)
   myArray.Add(CPoint(i, 2 * i));
for (int i = 0; i <= myArray.GetUpperBound(); i++)
{
   pt = myArray.GetAt(i);
   pt.x = 0;
   myArray.SetAt(i, pt);
}

CArray::GetSize() return the size off array.Because indexes are zero-based, the size is 1 greater than the largest index. Calling this method will generate the same result as the CArray::GetCount method.
C++
CArray<cpoint,cpoint> myArray;
for (int i = 0; i < 10; i++)
   myArray.Add(CPoint(i, 2*i));
for (int i = 0; i < myArray.GetSize(); i++)
{
   CPoint& pt = myArray.ElementAt(i);
   pt.x = 0;
}


You can call size() function for Vectors.
C++
#include <iostream>
#include <vector>
int main ()
{
  std::vector<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';
  for (int i=0; i<10; i++) myints.push_back(i);
  std::cout << "1. size: " << myints.size() << '\n';
  myints.insert (myints.end(),10,100);
  std::cout << "2. size: " << myints.size() << '\n';
  myints.pop_back();
  std::cout << "3. size: " << myints.size() << '\n';
  return 0;
}
 
Share this answer
 
v2
Comments
[no name] 9-Nov-15 1:40am    
thanks how to do with vector c++ can u let me know
[no name] 9-Nov-15 1:51am    
please see updated solution
[no name] 9-Nov-15 1:43am    
can i do size - 1
[no name] 9-Nov-15 1:51am    
yup sure. or you can use less than only as
for (int i = 0; i < myvector.size(); i++)
[no name] 9-Nov-15 1:53am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900