Output of Vector - c++

I was studying the library in C++ and I can't understand why my code isn't working... Can you help me please?
void Vect_Visualizza (vector<int> vect) {
for (auto it = vect.begin(); it != vect.end(); ++it)
cout << vect.at(it) << " ";
}

Very slight change.
void Vect_Visualizza (vector<int> vect) {
for (auto it = vect.begin(); it != vect.end(); ++it)
cout << *it << " ";
}
Think of the iterator as a pointer, not an index. begin() and end() return iterators. You'll want to read about them. They're confusing at first.
You can also do this:
for (int &i: vect) {
cout << i << endl;
}
Which is a lot easier unless you really, really need the iterator itself.

V.at(i) is same as V[i]
so it fails in your case because you are trying to use at() with iterator,which is wrong.
at() function expects an integer input, which is the index of the element we intend to access.
consider the code below:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
cout<<"indexed output: "<<v[1]<<"\n";
cout<<"output using at: "<<v.at(2)<<"\n";
}
output is:
indexed output: 2
output using at: 3
from this we can observe that using at() is same as indexed access.
Iterator works similar to pointer,hence you can write
cout<<*it

Related

Using iterators but with same amount of loc?

One can loop over a list by both:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> alist{1, 2, 3};
for (const auto& i : alist)
cout << i << endl;
list<int>::iterator i;
for (i = alist.begin(); i != alist.end(); i++)
cout << *i << endl;
return 0;
}
Mostly I don't use iterators because of the extra line of code I have to write, list<int>::iterator i;.
Is there anyway of not writing it? And still use iterator? Any new trick on newer C++ versions? Perhaps implementing my own list instead of using the one from stl?
Mostly I don't use iterators because of the extra line of code I have to write, list<int>::iterator i;.
You don't need to put it in an extra line. As with every for loop, you can define the iterator type inside of the parentheses, unless you'll need the value outside of the loops body.
So you can also write
for (list<int>::iterator i = alist.begin(); i != alist.end(); i++)
cout << *i << endl;
or
for (auto i = alist.begin(); i != alist.end(); i++)
cout << *i << endl;

Error during the usage of of size() function in vectors

So I've started learning vectors for the first time and wrote a simple program which goes like this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
int n;
cout<<"enter values"<<endl;
do
{
cin>>n;
g1.push_back(n);
} while (n);
cout<<"Vector values are: "<<endl;
for(auto i=g1.begin(); i<g1.size();i++)
cout<<*i<<endl;
}
When I try executing it, an error shows up saying "type mismatch" at the g1.size() part. Why exactly does this happen? I used the auto keyword for the iterator involved and assumed there wouldn't be any problem?
That is the bad side of using auto. If you have no idea what the result of auto is, you get no idea why it is something totally different you expect!
std::vector::begin delivers a std::vector::iterator and you can't compare it against an size_type value which is a result of std::vector::size. This type is typically std::size_t
You have to compare against another iterator which is the representation of the end of the vector like:
for(auto i = g1.begin(); i != g1.end(); i++)
There are at least three ways to iterate through the contents of a vector.
You can use an index:
for (int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << '\n';
You can use iterators:
for (auto it = vec.begin(); it != vec.end(); ++it)
std::cout << *it << '\n';
You can use a range-based for loop:
for (auto val : vec)
std::cout << Val <<'\n';
The latter two can be used with any container.
g1.begin() returns an iterator to the 1st element, whereas g1.size() returns the number of elements. You can't compare an iterator to a size, which is why you are getting the error. It has nothing to do with your use of auto, it has to do with you comparing 2 different things that are unrelated to each other.
You need to change your loop to compare your i iterator to the vector's end() iterator, eg:
for(auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << endl;
Or, simply use a range-based for loop instead, which uses iterators internally:
for(auto i : g1)
cout << i << endl;
Otherwise, if you want to use size() then use indexes with the vector's operator[], instead of using iterators, eg:
for(size_t i = 0; i < g1.size(); ++i)
cout << g1[i] << endl;

Print vector elements with iterator

I'm trying to print elements of a vector of list pair in a hash-table program in C++.
If I use the C++11 auto it's working but if i use a iterator
for (vector<int>::iterator i = arr_Hash[i].begin(); i != arr_Hash[i].end(); ++i)
//for (auto index = arr_Hash[i].begin(); index != arr_Hash[i].end(); index++)
{
cout << i->second;
cout << " ";
}
Error list: https://i.imgur.com/rDejBGG.png
How can I use the iterator here?
vector<int>::iterator i = arr_Hash[i].begin()
You're reusing the variable i here. Call it something else.
std::cout << i->second;
i is a std::vector<int>::iterator. Dereferencing it gives you an int&, which has no second member. You probably just want std::cout << *i;
The iterator for arr_Hash[i] needs to be on the same type as the vector.
Namely, if the type of arr_Hash[i] is vector<pair<int,int>> then it's iterator needs to be vector<pair<int,int>>::iterator.
Howerver, you should Prefer a range-for-statement to a for-statement when there is a choice.
for (auto& e : arr_Hash[i])
cout << i->second << " ";

how to print map in c++ without using iterator

Is it possible to print map in c++ without using iterator ?
something like
map <int, int>m;
m[0]=1;
m[1]=2;
for(int i =0; i<m.size(); i++)
std::cout << m[i];
Is it necessary to make iterator for printing map value ?
If you simply want to avoid typing out the iterator boilerplate, you can use a range-for loop to print each item:
#include <iostream>
#include <map>
int main() {
std::map<int,std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};
for (const auto& x : m) {
std::cout << x.first << ": " << x.second << "\n";
}
return 0;
}
Live example: http://coliru.stacked-crooked.com/a/b5f7eac88d67dafe
Ranged-for: http://en.cppreference.com/w/cpp/language/range-for
Obviously, this uses the map's iterators under the hood...
Is it necessary to make iterator for printing map value ?
Yes you need them, you can't know what keys been inserted in advance. And your code is not correct, think about
map <int, int>m;
m[2]=1;
m[3]=2;
for(int i =0; i<m.size(); i++)
std::cout << m[i]; // oops, there's not m[0] and m[1] at all.
// btw, std::map::operator[] will insert them in this case.

How to use find in linkedlist

Here is my code using STL library, where I try inserting a node at the end, in the middle and in front. For inserting in the middle, I want to provide insertion after a specific node, and not by incrementing the iterator by 2, as I might not know what to increment it by if it is a long list,
Kindly help why is find function not working:
#include <iostream>
#include <list>
#include <string>
using namespace std;
void printlist(list<int> l)
{
list<int>::iterator it = l.begin();
for (it; it != l.end(); ++it)
{
cout << "printlist function call list items: " << *it << endl;
}
}
int main()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
list<int>::iterator it = l.begin();
cout << 1 << endl;
printlist(l);
l.push_front(0);
cout << 2 << endl;
printlist(l);
it = l.find(l.begin(), l.end(), 2);
l.insert(it, 25);
cout << 3 << endl;
printlist(l);
return 0;
}
Thanks...
std::list<> doesn't have a find() method. You can use the standard algorithm std::find() declared in <algorithm>:
it = std::find(l.begin(), l.end(), 2);
See the answer by #0x499602D2.
But to elaborate on an important point raised in a comment by #NeilKirk, you wrote:
void printlist(list<int> l)
{
list<int>::iterator it = l.begin();
for (it; it != l.end(); ++it)
{
cout << "printlist function call list items: " << *it << endl;
}
}
Note that you are passing the list l by value, not by reference. Passing a class by value (that has not been designed to use implicit sharing) will make a copy. Thus, l will be a copy of the parameter passed. If your list contained a million elements, then passing it by value will make a million-element-copy. You can fix that with:
void printlist(list<int> & l) { ... }
Or if you don't plan on making any changes, it's always nice to announce that with:
void printlist(list<int> const & l) { ... }
Also, C++11 has a range-based for which does the iterator begin/end stuff under the hood for you, and automatic variable typing:
void printlist(list<int> const & l)
{
for (auto i : l)
{
cout << "printlist function call list items: " << i << endl;
}
}
Lots of ways to get fancy in that spirit. But the more critical thing is not go making copies of your data structures, passing them by value when you don't need to!