Welcome to this introductory lesson focused on Advanced Vector Operations without the use of built-in functions. While C++
provides a myriad of built-in functions to simplify vector operations, understanding the concepts behind these key functions significantly improves your ability to solve complex problems and prepares you for scenarios where built-in functions may not exist, or if they do, may not offer the optimal solution.
Consider a function, countOccurrences
, that takes in an array and its size as input, along with a target element. The function should return the number of times the target element appears in the array. We could use the std::count
function from the <algorithm>
header, but the task requires that we do not use built-in functions.
Our approach to the countOccurrences
function is:
-
Initialize Counter: Declare and initialize a variable
count
to zero. This will keep track of the occurrences of thetarget
in the array. -
Set Up a Loop: Utilize a
for
loop to iterate through each element of the array. The loop variablei
starts at zero and increments by one in each iteration, continuing untili
is less thansize
. -
Check for Target Match: Inside the loop, use an
if
statement to check if the current array elementarr[i]
is equal to thetarget
. -
Increment Counter if Match: If the current element matches the
target
, increment thecount
variable by one. -
Return Counter: After the loop completes, return the
count
variable, which now contains the total number of occurrences of thetarget
element in the array.
Here's the solution:
C++1#include <iostream> 2 3int countOccurrences(const int arr[], int size, int target) { 4 int count = 0; 5 for (int i = 0; i < size; ++i) { 6 if (arr[i] == target) { 7 ++count; 8 } 9 } 10 return count; 11} 12 13int main() { 14 int arr[] = {1, 2, 3, 2, 4, 2, 5}; 15 int size = sizeof(arr) / sizeof(arr[0]); 16 int target = 2; 17 18 int result = countOccurrences(arr, size, target); 19 std::cout << "The element " << target << " occurs " << result << " times." << std::endl; 20 21 return 0; 22} 23// Prints: The element 2 occurs 3 times.
Consider a function, findIndex
, that takes in a vector and a target element as input. The function should return the index of the first occurrence of the target element in the vector. If the target is not found, the function should return -1
. We could use the std::find
function from the <algorithm>
header, but the task requires we do not to use built-in functions.
Our approach to the findIndex
function is:
-
Set Up a Loop: Utilize a
for
loop to iterate through each element of the vector. The loop variablei
of starts at zero and increments by one in each iteration, continuing untili
is less than the size of the vector. -
Check for Target Match: Inside the loop, use an
if
statement to check if the current vector elementvec[i]
is equal to thetarget
. -
Return Index if Match: If the current element matches the
target
, return the current indexi
immediately, indicating the position of the first occurrence of thetarget
in the vector. -
Return -1 if Not Found: If the loop completes without finding the
target
element, return-1
to indicate that thetarget
is not present in the vector.
Here's the solution:
C++1#include <iostream> 2#include <vector> 3 4int findIndex(const std::vector<int>& vec, int target) { 5 for (size_t i = 0; i < vec.size(); ++i) { 6 if (vec[i] == target) { 7 return i; 8 } 9 } 10 return -1; // Return -1 if target not found 11} 12 13int main() { 14 std::vector<int> vec = {1, 2, 3, 2, 4, 2, 5}; 15 int target = 4; 16 17 int index = findIndex(vec, target); 18 if (index != -1) { 19 std::cout << "Element " << target << " found at index " << index << "." << std::endl; 20 } else { 21 std::cout << "Element " << target << " not found." << std::endl; 22 } 23 24 return 0; 25} 26// Prints: Element 4 found at index 4.
Grasping the concepts covered in this instruction is critical to succeeding in the practice exercises that follow, so take the time to understand these concepts thoroughly. Remember, we're not just learning algorithms but cultivating a deeper understanding of how we can break down and solve complex problems with relatively simple code. Therefore, get ready and anticipate an exciting, revealing practice session!