How to iterate first ten items from the container - c++

I want to print only first 10 inserted items while leaving the rest behind. What code do i have to use (instead of using myset.end() ) to print only first 10 integers instead of printing every single integer.
int main ()
{
std::set<int> myset;
std::set<int>::iterator it;
// set some initial values:
for (int i=1; i<=20; ++i)
myset.insert(i*10);
std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << *it << ' ';
std::cout << "\n\n";
return 0;
}

You might use std::next as follow:
const auto begin = myset.begin();
const auto end = myset.size() < 10 ? myset.end() : std::next(begin, 10);
for (auto it = begin; it != end; ++it) {
std::cout << *it << ' ';
}

You can just keep a count variable and break the loop as soon as it reaches 10 or you reach myset.end().
int main ()
{
std::set<int> myset;
std::set<int>::iterator it;
// set some initial values:
for (int i=1; i<=20; ++i)
myset.insert(i*10);
int count = 0;
std::cout << "myset contains:";
for (it=myset.begin(); count < 10 && it!=myset.end(); ++it, ++count)
std::cout << *it << ' ';
std::cout << "\n\n";
return 0;
}

I would do it like this:
int nCount = 0;
for (it=myset.begin(); nCount<10 && it!=myset.end(); ++nCount, ++it)
{
std::cout << *it << ' ';
}
Hope this helps.

Related

iterator a set in array c++

#include <iostream>
#include <unordered_set>
using namespace std;
void arraySet(unordered_set<int> n[]){
for(auto it1 = n->begin(); it1!= n->end(); it1++){
cout << "n[" <<*it1<<"]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
int main()
{
unordered_set<int> n[3];
n[0].insert(734);
n[0].insert(23);
n[0].insert(634);
n[1].insert(2);
n[1].insert(1);
n[2].insert(1);
arraySet(n);
return 0;
}
Can anyone help me explain how to iterator through this set inside array. I believe an easy way is to convert it to set inside vector.
Size of the array needs to be passed as well to the function for you to be able to iterate through all the sets of the array. With just passing the pointer, size cannot be determined and dereferencing n->begin will iterate through only the first set.
void arraySet(unordered_set<int> n[], int size) {
for (auto i = 0; i < size; i++) {
for (auto it1 = n[i].begin(); it1 != n[i].end(); it1++) {
cout << "n[" << *it1 << "]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
}
int main()
{
unordered_set<int> n[3];
n[0].insert(734);
n[0].insert(23);
n[0].insert(634);
n[1].insert(2);
n[1].insert(1);
n[2].insert(1);
arraySet(n,3);
return 0;
}
Or you could use std::vector to contain the sets and pass it instead.
std::vector<unordered_set<int>> n(3); // Sets the size to 3 elements
The function definition would be changed to
void arraySet(std::vector<unordered_set<int>>& n) {
for (size_t i = 0; i < n.size(); i++) {
for (auto it1 = n[i].begin(); it1 != n[i].end(); it1++) {
cout << "n[" << *it1 << "]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
}

Inserting list elements to vector, getting additional unwanted values C++

In my C++ script, I want to insert some elements of a list into a vector (from the beginning of list to a specific position "it"), and then try to add the vector at the top of the list and keeping the same order of the vector but I get unwanted additional elements in the vector.
Here is my code:
#include <iostream>
#include <iterator>
#include <vector>
#include <list>
int main() {
std::list<int> mylist;
for (int i = 0; i < 10; i++)
mylist.push_back(i * 10);
for (std::list<int>::iterator i = mylist.begin(); i <= mylist.end(); ++i) {
std::cout << *i << ", ";
}
std::cout << std::endl;
std::advance(it, 6);
std::cout << "The 6th element in mylist is: " << *it << std::endl;
// The vector that will contain mylist elements
std::vector<int> intAdeplacer;
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
std::cout << std::endl;
std::cout << "Nombre d'éléments dans le vecteur : " << intAdeplacer.size() << std::endl;
std::cout << std::endl;
// see the content of the vector
std::cout << "Le vecteur de deplacement contient : " << std::endl;
for (std::vector<int>::const_iterator i = intAdeplacer.begin();
i <= intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
I get this output:
Le vecteur de deplacement contient :
0, 10, 20, 30, 40, 134985,
134985 is not wanted..
// Insert in front of the list the values of the vector and keeping the same order of elements in the vector
for (std::vector<int>::const_iterator i = intAdeplacer.end();
i >= intAdeplacer.begin(); --i) {
mylist.push_front(*i);
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "nouvelle composition de mylist : " << std::endl;
for (std::list<int>::const_iterator j = mylist.begin(); j != mylist.end();
++j) {
std::cout << *j << ", ";
}
std::cout << std::endl;
std::cout << std::endl;
// erasing the added elements from mylist
std::list<int>::iterator debut = mylist.begin();
std::list<int>::iterator fin = mylist.end();
std::advance(fin, 6);
mylist.erase(debut, fin);
for (std::list<int>::iterator j = mylist.begin(); j <= mylist.end(); ++j) {
std::cout << *j << ", ";
}
return 0;
}
If it is an iterator pointing at the 6th element of your list, the following insert() will insert from begin() (included) to it (excluded) into the vector:
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
So you'll have only 5 elements, from 0 to 40. Unfortunately your printing loop does include the end() of the vector, which is out of range. This is why you get this strange trailing number.
Just correct your loop into:
for (auto i = intAdeplacer.begin(); i != intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
Or consider this alternate range-for syntax:
for (auto &element: intAdeplacer) {
std::cout << element << ", ";
}

Sorting vector to bifurcate even and odd numbers

I am trying to sort a vector using sort so that all the even numbers and odd numbers are brought together but somehow that doesnt seem to work.
Sample code below.
#include <iostream>
#include <algorithm>
#include <vector>
bool myfunction (int i,int j) { return ((i&2)>(j&2)); }
bool yourfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8);
int count=0;
// using function as comp
std::sort (myvector.begin(), myvector.end(), myfunction);
for (std::vector<int>::iterator it=myvector.begin();it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
if(((*it)&2)==0)
{
break;
}
count++;
}
std::cout << "myvector contains after 1st sort:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::sort (myvector.begin()+count, myvector.end(), yourfunction);
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
You can use the function std::partition to do this.
auto oddStart = std::partition(std::begin(myints),
std::end(myints),
[](int i){ return i % 2 == 0; });
After this your even values are from
for(auto it = std::begin(myints); it != oddStart; ++it)
and the odds are
for(auto it = oddStart; it != std::end(myints); ++it)
If you want to stick to using std::sort you can use this function:
bool myfunction (int i,int j) { return ((i % 2) > (j % 2)); }
Running sort with your input results in:
71
45
53
33 //ODD
32 //EVEN
12
26
80

forloop, iterators and vectors doesn't cooperate with me

Why does this work fine for me:
for(int i = 0; i < vec.size(); i++)
{
os << vec[i] << " ";
}
while this doesn't:
for(vector<int>::iterator it = vec.begin(); it < vec.end(); it++)
{
os << vec[*it] << " ";
}
You should be printing *it instead of using it as the index and you should probably change the condition to it != vec.end().
You're using the iterator wrong, it should be:
for(vector<int>::iterator it = vec.begin(); it < vec.end(); it++)
{
os << *it << " ";
}
Your code just attempts to print the element at index *it, which might not even be valid.

How to print a vector array?

I have a vector array called nVectors.
vector<int>* nVectors[21];
for (int i = 1; i <= 20; i ++) {
nVectors[i] = generateVector(i);
}
I can print all the members of a single vector, but when it comes to the vector array, I still don't know how to print all the vectors in an array.
Maybe an iterator through all the member of a vector array and print using my predefined method pvector can solve this problem? But I don't know how to iterate in gdb.
std::array<std::vector<int>*, 21> nVectors;
for(std::array<std::vector<int>*>::iterator i = nVectors.begin();
i != nVectors.end();
++i)
{
for(std::vector<int>::iterator it = (*i)->begin();
it != (*i)->end();
++it)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;
Or, in C++11:
std::vector<int>* nVectors[21];
for(auto &i : nVectors)
{
for(auto &it : i)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;