Re: vector representation
"Andrea Crotti" <andrea.crotti.0@gmail.com> wrote in message
news:2a00c76f-d8cd-4f9b-9096-b7cb8032370e@l39g2000yqh.googlegroups.com...
Alright I tried like this
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <sstream>
template<typename T, typename Repr=T>
std::string vectorToString(const std::vector<T>& inp) {
std::string empty("[]");
if (inp.size() == 0)
return empty;
std::ostringstream os;
os<< "[";
size_t i;
for (i=0; i< (inp.size()-1); ++i) {
os<< static_cast<Repr> (inp[i])<< ",";
}
os<< static_cast<Repr> (inp[i])<< "]";
return os.str();
}
int main()
{
std::vector<char> v {1, 2, 3};
std::cout << vectorToString<char,unsigned int>( v ) << std::endl;
}
and then yes, it's correct already, I'm probably doing something else
stupid when calling the function then...
Thanks
If you want to convert to a vector of int , why do you return a string?
The conversion is only temporary and you do not see what its converted to,
what you need to do is something like this :
#include <iostream>
#include <string>
#include <vector>
template< template<typename T1, typename Alloc1> class C1, typename T1,
typename Alloc1,
template<typename T2, typename Alloc2> class C2, typename T2, typename
Alloc2>
void convert(const C1<T1, Alloc1>& vchar, C2<T2, Alloc2>& vint) {
for (std::size_t i=0; i<vchar.size(); ++i){
vint[i] = 0xF & static_cast<T2>(vchar[i]);
/*Mask the ascii value with 0xF to convert to int.*/
}
}
int main()
{
std::vector<char> vc(3);
vc[0]='1';
vc[1]='2';
vc[2]='3';
std::vector<int> vi(3);
convert(vc, vi);
for (int i=0;i<3; ++i ){
std::cout<< vi[i] <<'\t';
}
}