How to print a vector array? - c++

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;

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;
}
}
}

access map in vector c++

Does anyone know how I can access map key/value when the map is in the vector?
struct ControlPointSet
{
std::map<uint32_t,uint32_t> control_points;
}
When the vector look like this:
void someMethod(const std::vector<ControlPointSet>& controlpoint_sets)
{
//run through the map vector
for(auto it = controlpoint_sets.begin(); it != controlpoint_sets.end(); it++)
{
for(int i = 0; i < it->control_points.size(); i ++)
{
std::cout << it->control_points.at(i) << std::endl;
}
}
Somehow this is not working
You cannot access a std::map's elements by index. Its at() method takes a key as input instead.
You can use an iterator:
void someMethod(const std::vector<ControlPointSet>& controlpoint_sets)
{
//run through the elements of the vector
for(auto it = controlpoint_sets.begin(); it != controlpoint_sets.end(); ++it)
{
//run through the elements of a map
for(auto cp = it->control_points.begin(); cp != it->control_points.end(); ++cp)
{
std::cout << cp->first << " " << cp->second << std::endl;
}
}
}
Or a range-based for loop:
void someMethod(const std::vector<ControlPointSet>& controlpoint_sets)
{
//run through the elements of the vector
for(const auto &cps : controlpoint_sets)
{
//run through the elements of a map
for(const auto &cp : cps.control_points)
{
std::cout << cp.first << " " << cp.second << std::endl;
}
}
}

How to insert multiple String in an Array

In C++ how can I insert multiple string in an array and print it, like:
Array[] = {"One", "Two", "Three", "Four".......};
I want to print them according to their index location.
Thank you.
If you want to use raw array, should be:
std::string strArr[] = { "One","Two","Three","Four" };
for (size_t i = 0; i < sizeof(strArr) / sizeof(std::string); i++)
{
std::cout << strArr[i] << " ";
}
std::cout << std::endl;
It's better to use vector instead of raw array:
std::vector<std::string> strVec = { "One","Two","Three","Four" };
for (size_t i = 0; i < strVec.size(); i++)
{
std::cout << strVec[i] << " ";
}
std::cout << std::endl;
Another way to traverse vector is to use iterator:
for (auto itr = strVec.begin(); itr != strVec.end(); itr++)
{
std::cout << *itr << " ";
}
std::cout << std::endl;
You can use standard vector to achieve your goal.
std::vector<std::string> stringVector = {"One","Two","Three","Four"}<br/>
std::vector can also be extended after its allocated.

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 << ", ";
}

Print a vector of maps

how do I print a vector of multimaps?
for example I have a vector that looks like this:
typedef std::multimap<double,std::string> resultMap;
typedef std::vector<resultMap> vector_results;
EDIT
for(vector_results::iterator i = vector_maps.begin(); i!= vector_maps.end(); i++)
{
for(vector_results::iterator j = i->first.begin(); j!= i->second.end(); j++)
{
std::cout << j->first << " " << j->second <<std::endl;
}
}
The i variable in the outer for loop points to a resultMap, which means the type of the j variable needs to be resultMap::iterator. Rewrite your loops as
for(vector_results::const_iterator i = vector_maps.begin(); i != vector_maps.end(); ++i)
{
for(resultMap::const_iterator j = i->begin(); j != i->end(); ++j)
{
std::cout << j->first << " " << j->second << std::endl;
}
}
I changed the iterator type to const_iterator since the iterators are not modifying the container elements.
If your compiler supports C++11's range based for loops, the code can be written much more succinctly
for( auto const& i : vector_maps ) {
for( auto const& j : i ) {
std::cout << j.first << " " << j.second << std::endl;
}
}