Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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.
 
std::vector< int32_t > shift_left (const std::vector< int32_t > &array, size_t shift)
 Shifts the given vector to the left by the shift amount and returns a new vector with the result. The original vector is not mutated.
 
std::vector< int32_t > shift_right (const std::vector< int32_t > &array, size_t shift)
 Shifts the given vector to the right by the shift amount and returns a new vector with the result. The original vector is not mutated.
 
std::vector< int32_t > get_intersection (const std::vector< int32_t > &first, const std::vector< int32_t > &second)
 Gets the intersection of two sorted arrays, and returns them in a vector.
 
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.
 

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

Operations on data structures

for std::sort 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

Function Documentation

◆ get_intersection()

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

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

An algorithm is used that compares the elements of the two vectors, incrementing the index of the smaller of the two. If the elements are the same, the element is appended to the result array to be returned.

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

< Vector to hold the intersection

< Index for the first array

< Index for the second array

< Length of first array

< Length of second array

< Increment index of second array

< Increment index of second array

< Add the element if it is unique

< Increment index of first array

< Increment index of second array too

50 {
51 std::vector<int32_t> res; ///< Vector to hold the intersection
52 size_t f_index = 0; ///< Index for the first array
53 size_t s_index = 0; ///< Index for the second array
54 size_t f_length = first.size(); ///< Length of first array
55 size_t s_length = second.size(); ///< Length of second array
56
57 while (f_index < f_length && s_index < s_length) {
58 if (first[f_index] < second[s_index]) {
59 f_index++; ///< Increment index of second array
60 } else if (first[f_index] > second[s_index]) {
61 s_index++; ///< Increment index of second array
62 } else {
63 if ((res.size() == 0) || (first[f_index] != res.back())) {
64 res.push_back(
65 first[f_index]); ///< Add the element if it is unique
66 }
67 f_index++; ///< Increment index of first array
68 s_index++; ///< Increment index of second array too
69 }
70 }
71 return res;
72}
T back(T... args)
T push_back(T... args)
T size(T... args)
Here is the call graph for this function:

◆ 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

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

Print each value in the array

Print newline

Print each value in the array

Print newline

Print each value in the array

Print newline

Print each value in the array

Print newline

Print each value in the array

Print newline

Print each value in the array

Print newline

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

◆ shift_left()

std::vector< int32_t > operations_on_datastructures::shift_left ( const std::vector< int32_t > & array,
size_t shift )

Shifts the given vector to the left by the shift amount and returns a new vector with the result. The original vector is not mutated.

Shifts the values of the vector, by creating a new vector and adding values from the shift index to the end, then appending the rest of the elements from the start of the vector.

Parameters
arrayA reference to the input std::vector
shiftThe amount to be shifted to the left
Returns
A std::vector with the shifted values

< We got an invalid shift, return empty array

< Result array

< Add values after the shift index

< Add the values from the start

47 {
48 if (array.size() <= shift) {
49 return {}; ///< We got an invalid shift, return empty array
50 }
51 std::vector<int32_t> res(array.size()); ///< Result array
52 for (size_t i = shift; i < array.size(); i++) {
53 res[i - shift] = array[i]; ///< Add values after the shift index
54 }
55 for (size_t i = 0; i < shift; i++) {
56 res[array.size() - shift + i] =
57 array[i]; ///< Add the values from the start
58 }
59 return res;
60}
Here is the call graph for this function:

◆ shift_right()

std::vector< int32_t > operations_on_datastructures::shift_right ( const std::vector< int32_t > & array,
size_t shift )

Shifts the given vector to the right by the shift amount and returns a new vector with the result. The original vector is not mutated.

Shifts the values of the vector, by creating a new vector and adding values from the shift index to the end, then appending the rest of the elements to the start of the vector.

Parameters
arrayA reference to the input std::vector
shiftThe amount to be shifted to the right
Returns
A std::vector with the shifted values

< We got an invalid shift, return empty array

< Result array

< Add values after the shift index

< Add the values from the start

48 {
49 if (array.size() <= shift) {
50 return {}; ///< We got an invalid shift, return empty array
51 }
52 std::vector<int32_t> res(array.size()); ///< Result array
53 for (size_t i = shift; i < array.size(); i++) {
54 res[i] = array[i - shift]; ///< Add values after the shift index
55 }
56 for (size_t i = 0; i < shift; i++) {
57 res[i] =
58 array[array.size() - shift + i]; ///< Add the values from the start
59 }
60 return res;
61}
Here is the call graph for this function: