Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
operations_on_datastructures Namespace Reference

for std::vector More...

Functions

void print (const std::vector< int32_t > &array)
 Prints the values of a vector sequentially, ending with a newline character. More...
 
std::vector< int32_t > get_union (const std::vector< int32_t > &first, const std::vector< int32_t > &second)
 Gets the union of two sorted arrays, and returns them in a vector. More...
 

Detailed Description

for std::vector

for std::priority_queue

For std::vector.

for assert for IO Operations

Operations on data structures

For assert For IO operations For std::queue

Operations on Data Structures

for std::count for assert for tolower for string operations for IO Operations

Operations on data structures

for std::sort for assert for IO operations

Operations on Data Structures

Function Documentation

◆ get_union()

std::vector< int32_t > operations_on_datastructures::get_union ( const std::vector< int32_t > &  first,
const std::vector< int32_t > &  second 
)

Gets the union of two sorted arrays, and returns them in a vector.

An algorithm is used that compares the elements of the two vectors, appending the one that has a lower value, and incrementing the index for that array. If one of the arrays reaches its end, all the elements of the other are appended to the resultant vector.

Parameters
firstA std::vector of sorted integer values
secondA std::vector of sorted integer values
Returns
A std::vector of the union of the two arrays, in ascending order

< Vector to hold the union

< Index for the first array

< Index for the second array

< Length of first array

< Length of second array

< Integer to store value of the next element

< Append from first array

< Increment index of second array

< Append from second array

< Increment index of second array

< Element is the same in both

< Increment index of first array

< Increment index of second array too

< Add the element if it is unique

< Add remaining elements

< Add the element if it is unique

< Add remaining elements

< Add the element if it is unique

49 {
50 std::vector<int32_t> res; ///< Vector to hold the union
51 size_t f_index = 0; ///< Index for the first array
52 size_t s_index = 0; ///< Index for the second array
53 size_t f_length = first.size(); ///< Length of first array
54 size_t s_length = second.size(); ///< Length of second array
55 int32_t next = 0; ///< Integer to store value of the next element
56
57 while (f_index < f_length && s_index < s_length) {
58 if (first[f_index] < second[s_index]) {
59 next = first[f_index]; ///< Append from first array
60 f_index++; ///< Increment index of second array
61 } else if (first[f_index] > second[s_index]) {
62 next = second[s_index]; ///< Append from second array
63 s_index++; ///< Increment index of second array
64 } else {
65 next = first[f_index]; ///< Element is the same in both
66 f_index++; ///< Increment index of first array
67 s_index++; ///< Increment index of second array too
68 }
69 if ((res.size() == 0) || (next != res.back())) {
70 res.push_back(next); ///< Add the element if it is unique
71 }
72 }
73 while (f_index < f_length) {
74 next = first[f_index]; ///< Add remaining elements
75 if ((res.size() == 0) || (next != res.back())) {
76 res.push_back(next); ///< Add the element if it is unique
77 }
78 f_index++;
79 }
80 while (s_index < s_length) {
81 next = second[s_index]; ///< Add remaining elements
82 if ((res.size() == 0) || (next != res.back())) {
83 res.push_back(next); ///< Add the element if it is unique
84 }
85 s_index++;
86 }
87 return res;
88}
T back(T... args)
T next(T... args)
T push_back(T... args)
T size(T... args)
Here is the call graph for this function:

◆ print()

void operations_on_datastructures::print ( const std::vector< int32_t > &  array)

Prints the values of a vector sequentially, ending with a newline character.

Parameters
arrayReference to the array to be printed
Returns
void

Print each value in the array

Print newline

30 {
31 for (int64_t i : array) {
32 std::cout << i << " "; /// Print each value in the array
33 }
34 std::cout << "\n"; /// Print newline
35}