Re: cout << vector<string>
"Jeff Schwab" <jeff@schwabcenter.com> ha scritto nel messaggio
news:Q72dnf5yWqjvqInUnZ2dnUVZ_vCdnZ2d@giganews.com...
Pete Becker wrote:
On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegorushkin@gmail.com> said:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
template<typename T>
std::ostream& operator<<(std::ostream& out, std::vector<T> const& v) {
if (!v.empty()) {
typedef std::ostream_iterator<T> out_iter;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
what is the meaning of "out << v.back();"
why not write above only?
copy(v.begin(), v.end(), out_iter( out, " " ));
why it is not easy?
is it not better the "operator<<" defined below?
#include <stdio.h>
#include <iostream.h>
#include <iterator.h>
#include <vector.h>
template<typename T>
std::ostream& operator<<(std::ostream& out, std::vector<T>const& v)
{size_t i;
for(i=0; i<v.size(); ++i)
out<<v[i]<<" ";
return out;
}
int main()
{int ints[]={1,2,3,4}, inty2[100]={0}, i;
std::vector<int> vettore(inty2, inty2+100);
std::vector<int> vettor1(100);
for(i=0; i<90; ++i)
vettor1[i]=i;
std::cout << std::vector<int>(ints,ints+4) << '\n';
std::cout << vettore << '\n';
std::cout << "Vettor1" << vettor1 << '\n';
getchar();
}
the above seems ok what not seems ok to me that this
"
std::vector<int> vettor1;
for(i=0; i<90; ++i)
vettor1[i]=i;
"
here goes in segmentation fault
is not vector1 self sizabile?
than how you are sure that *all* the allocations have its deallocation?
for example it seems "std::vector<int>(ints,ints+4)"
copy in new memory the array ints[0..3]
and than it has to deallocate it but when and where
}
return out;
}
int main() {
int const ints[] = { 1, 2, 3, 4 };
std::cout << std::vector<int>( ints, ints + 4 ) << '\n';
}