Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 23, 2015 07:44
Show Gist options
  • Save krysseltillada/51df809459cd0ca89b7a to your computer and use it in GitHub Desktop.
Save krysseltillada/51df809459cd0ca89b7a to your computer and use it in GitHub Desktop.
comparing containers
#include <iostream> /// std::cout; /// std::endl;
#include <vector> /// std::vector <t>
#include <list> /// std::list <t>
int main ()
{
std::list <int> il = {1, 3, 3, 4, 5};
std::vector <int> v1 = {1, 2, 3, 22, 5};
if (il.size() == v1.size()) { /// if the two containers are the same
auto il_beg = il.begin(), il_end = il.end(); /// get the range of the list (denotes at the begin to end of last)
auto v1_beg = v1.begin(), v1_end = v1.end(); /// get the range of the list (same)
for (; il_beg != il_end && v1_beg != v1_end; ++il_beg, ++v1_beg) { /// compares each element in the two containers
if (*il_beg == *v1_beg) {
std::cout << "il at element: " << std::distance (il.begin(), il_beg) << " is equal to " /// gets the index number
<< "v1 at element: " << std::distance (v1.begin(), v1_beg) << std::endl;
}
else {
std::cout << "il at element: " << std::distance (il.begin(), il_beg) << " is not equal to "
<< "v1 at element: " << std::distance (v1.begin(), v1_beg) << std::endl;
}
}
} else { /// if the size of the two is not the same
std::cout << "not equal " << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment