Devel Lib
Devel::VectorUtils Namespace Reference

This namespace includes utilities for working with vectors. More...

Functions

template<typename T >
bool IsDataInVector (const std::vector< T > &i_atVector, const T &i_tData, size_t *i_pOutIndex=nullptr)
 This function checks if a given data is present in the vector. More...
 
template<typename T >
bool IsDataInSortedVector (const std::vector< T > &i_atVector, const T &i_tData, size_t *i_pOutIndex=nullptr)
 This function checks if a given data is present in the sorted vector. More...
 

Detailed Description

This namespace includes utilities for working with vectors.

Example

std::vector<int> vec = {1, 2, 3, 4, 5};
int data = 3;
size_t index = 0;
// Check if the data is in the vector
bool isInVector = Devel::VectorUtils::IsDataInVector(vec, data, &index);
// If the data is in the vector, print its index
if (isInVector) {
std::cout << "Data found at index: " << index << std::endl;
} else {
std::cout << "Data not found in vector" << std::endl;
}
bool IsDataInVector(const std::vector< T > &i_atVector, const T &i_tData, size_t *i_pOutIndex=nullptr)
This function checks if a given data is present in the vector.
Definition: VectorUtils.h:36

Function Documentation

◆ IsDataInSortedVector()

template<typename T >
bool Devel::VectorUtils::IsDataInSortedVector ( const std::vector< T > &  i_atVector,
const T &  i_tData,
size_t *  i_pOutIndex = nullptr 
)

This function checks if a given data is present in the sorted vector.

Your vector must be sorted in ascending order for this function to work. It uses std::lower_bound to locate the input data in the vector. If it finds a match, it sets the index of the matching element to the out index (if provided) and returns true.

Parameters
[in]i_atVectorThe input vector.
[in]i_tDataThe data to search for in the vector.
[out]i_pOutIndexPointer to an index variable where the index of the found element will be stored. If this parameter is not provided, the function simply checks if the data exists in the vector.
Returns
Returns true if the data is found in the vector, false otherwise.

◆ IsDataInVector()

template<typename T >
bool Devel::VectorUtils::IsDataInVector ( const std::vector< T > &  i_atVector,
const T &  i_tData,
size_t *  i_pOutIndex = nullptr 
)

This function checks if a given data is present in the vector.

It uses std::find to locate the input data in the vector. If it finds a match, it sets the index of the matching element to the out index (if provided) and returns true.

Parameters
[in]i_atVectorThe input vector.
[in]i_tDataThe data to search for in the vector.
[out]i_pOutIndexPointer to an index variable where the index of the found element will be stored. If this parameter is not provided, the function simply checks if the data exists in the vector.
Returns
Returns true if the data is found in the vector, false otherwise.