I was implementing binary search and I run into an interesting problem in C++
vector<int> nums;
nums.reserve(30);
nums[0] = 1;
for (int index = 1; index < 30; index++)
{
nums[index] = // assign a random number
}
bool flag = binarySearch(v1, 57);
The problem with this code was:I reserved memory for 30 elements and wrote into the memory. It looked like nums had 30 numbers written into memory. But the size was zero.
When I implemented a binary search function the size returned zerobool binarySearch(vector<int>& nums, int k)
{
...
high = nums.size();
...
}
No comments:
Post a Comment