A
arnuld
i wrote a programme to create a vector of 5 elements (0 to 4), here is
the code & output:
#include <iostream>
#include <vector>
int main() {
std::vector<int> ivec;
// dynamically create a vector
for(std::vector<int>::size_type ix = 0; ix <= 4; ++ix)
{
ivec.push_back(ix);
}
// print-out the elements
for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
{
std::cout << "Element: " << *iter << "..." << std::endl;
}
// create a middle iterator
std::vector<int>::iterator mid_iter = (ivec.begin() + ivec.size() /
2);
// print it out to check where it points
std::cout << "middle element: " << *mid_iter << std::endl;
return 0;
}
OUTPUT:
Element: 0...
Element: 1...
Element: 2...
Element: 3...
Element: 4...
middle element: 2 // fine
now if i change the "mid_iter" to:
/std::vector<int>::iterator mid_iter = (ivec.size() / 2);/
then i got an ERROR:
----------------------------------------------------------------
unix@debian:~/Desktop$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:22: error: conversion from `unsigned int' to non-scalar type `
__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >'
requested
unix@debian:~/Desktop$
----------------------------------------------------------------
it is showing "non-scalar type". what is that? what is happening
"behind the curtains"?
"arnuld"
the code & output:
#include <iostream>
#include <vector>
int main() {
std::vector<int> ivec;
// dynamically create a vector
for(std::vector<int>::size_type ix = 0; ix <= 4; ++ix)
{
ivec.push_back(ix);
}
// print-out the elements
for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
{
std::cout << "Element: " << *iter << "..." << std::endl;
}
// create a middle iterator
std::vector<int>::iterator mid_iter = (ivec.begin() + ivec.size() /
2);
// print it out to check where it points
std::cout << "middle element: " << *mid_iter << std::endl;
return 0;
}
OUTPUT:
Element: 0...
Element: 1...
Element: 2...
Element: 3...
Element: 4...
middle element: 2 // fine
now if i change the "mid_iter" to:
/std::vector<int>::iterator mid_iter = (ivec.size() / 2);/
then i got an ERROR:
----------------------------------------------------------------
unix@debian:~/Desktop$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:22: error: conversion from `unsigned int' to non-scalar type `
__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >'
requested
unix@debian:~/Desktop$
----------------------------------------------------------------
it is showing "non-scalar type". what is that? what is happening
"behind the curtains"?
"arnuld"