How do I declare the same type of it in the for statement in the code below?
It doesn't come out properly even if I use typeid
My prediction is std::deque::iterator, is that right?
#include <iostream>
#include <deque>
#include <typeinfo>
using namespace std;
int main(){
deque<int> dep;
dep.push_back(10);
int nSum=0;
for(auto it = dep.begin(); it != dep.end(); it++){
nSum += (*it);
cout << typeid(it).name() << endl;
}
cout << nSum <<endl;
return 0;
}
You can define the type, such as std::deque<int>::iterator it
.
Looking at the definition of begin in the decque, it comes out.
// deque::begin
#include <iostream>
#include <deque>
int main ()
{
std::deque<int> mydeque;
for (int i=1; i<=5; i++) mydeque.push_back(i);
std::cout << "mydeque contains:";
std::deque<int>::iterator it = mydeque.begin();
while (it != mydeque.end())
std::cout << ' ' << *it++;
std::cout << '\n';
return 0;
}
© 2024 OneMinuteCode. All rights reserved.