Name: Anonymous 2010-12-02 23:00
vector iteration, working in one instance, not working in the next. I don't understand.
% cat vector.cpp
#include <iostream>
#include <cstdio>
#include <vector>
template<typename T>
std::ostream &operator<<(std::ostream &o, std::vector<T> &v)
{
for(typename std::vector<T>::iterator it = v.begin(); it != v.end(); ++it)
std::cout << *it;
}
int main(void)
{
std::vector<int> v;
std::cout.sync_with_stdio();
setbuf(stdout, NULL);
for(int i = 0; i < 10; i++)
v.push_back(i);
for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
std::cout << v << std::endl;
return 0;
}
% make vector
g++ -g -o vector vector.cpp
% ./vector
0
1
2
3
4
5
6
7
8
9
0123456789zsh: segmentation fault ./vector
%
% cat vector.cpp
#include <iostream>
#include <cstdio>
#include <vector>
template<typename T>
std::ostream &operator<<(std::ostream &o, std::vector<T> &v)
{
for(typename std::vector<T>::iterator it = v.begin(); it != v.end(); ++it)
std::cout << *it;
}
int main(void)
{
std::vector<int> v;
std::cout.sync_with_stdio();
setbuf(stdout, NULL);
for(int i = 0; i < 10; i++)
v.push_back(i);
for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
std::cout << v << std::endl;
return 0;
}
% make vector
g++ -g -o vector vector.cpp
% ./vector
0
1
2
3
4
5
6
7
8
9
0123456789zsh: segmentation fault ./vector
%