vector representation

A

Andrea Crotti

I have the following problem, I have a vector of chars and I want to
represent it as
a vector of ints.
So I added another parameter in the template function which defaults
to T (gcc needs the new 0++x standard for this) but still it doesn't
work

Here below the code, the function at run time is correct
"vectorToString<char, unsigned int>"
but I always see printed out the char representation.
Anything else wrong?


template <typename T, typename Repr=T>
std::string vectorToString(const std::vector<T>& inp) {
std::string empty("[]");
if (inp.size() == 0)
return empty;

std::eek:stringstream os;
os << "[";
size_t i;
for (i=0; i < (inp.size()-1); ++i) {
os << static_cast<Repr> (inp) << ",";
}
os << static_cast<Repr> (inp) << "]";
return os.str();
}


Thanks,
Andrea
 
I

Ian Collins

I have the following problem, I have a vector of chars and I want to
represent it as
a vector of ints.
So I added another parameter in the template function which defaults
to T (gcc needs the new 0++x standard for this) but still it doesn't
work

Here below the code, the function at run time is correct
"vectorToString<char, unsigned int>"
but I always see printed out the char representation.
Anything else wrong?


template<typename T, typename Repr=T>

Function template default parameters are a C++0x feature.
std::string vectorToString(const std::vector<T>& inp) {
std::string empty("[]");
if (inp.size() == 0)
return empty;

std::eek:stringstream os;
os<< "[";
size_t i;
for (i=0; i< (inp.size()-1); ++i) {
os<< static_cast<Repr> (inp)<< ",";
}
os<< static_cast<Repr> (inp)<< "]";
return os.str();
}


What doesn't work? I tried this with gcc 4.5.1:

int main()
{
std::vector<char> v {'a', 'b','c'};

std::cout << vectorToString<char,unsigned int>( v ) << std::endl;
}

and it gives the expected

[97,98,99]
 
P

Paul

Andrea Crotti said:
I have the following problem, I have a vector of chars and I want to
represent it as
a vector of ints.
So I added another parameter in the template function which defaults
to T (gcc needs the new 0++x standard for this) but still it doesn't
work
What do you mean by "represented as a vector of int"?

Does the char array "A" == 65? In other words a direct ascii conversion
Does the char array "65" == two integers (6 & 5), or one int(65).
Whats happens if you have "65.6"? Is it rounded up ?

Conversion from string to vector of int aint so simple :) I know you say
"vector of char" above but your code below uses strings, so I dunno if chars
are treated individually or as strings.
Here below the code, the function at run time is correct
"vectorToString<char, unsigned int>"
but I always see printed out the char representation.
Anything else wrong?

Output to screen is always going to be chars. How can it be anything else?
:)
Maybe its better to assign to a vector of int, and examine this in a
debugger , for testing purposes. Screen output can introduce another
conversion.
template <typename T, typename Repr=T>
std::string vectorToString(const std::vector<T>& inp) {
std::string empty("[]");
if (inp.size() == 0)
return empty;

std::eek:stringstream os;
os << "[";
size_t i;
for (i=0; i < (inp.size()-1); ++i) {
os << static_cast<Repr> (inp) << ",";
}
os << static_cast<Repr> (inp) << "]";
return os.str();
}


Thanks,
Andrea
 
A

Andrea Crotti

What doesn't work?  I tried this with gcc 4.5.1:

int main()
{
   std::vector<char> v {'a', 'b','c'};

   std::cout << vectorToString<char,unsigned int>( v ) << std::endl;

}

and it gives the expected

[97,98,99]

Ah ok, well that's not what I meant, that's why it probably doesn't
"work" for me..
I just want to have something like
std::vector<char> v{1, 2, 3};
and print out
[1, 2, 3]
instead of the char corresponding to 1, 2, 3 in the ASCII table, which
is what happens now..
 
A

Andrea Crotti

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::eek:stringstream os;
os<< "[";
size_t i;
for (i=0; i< (inp.size()-1); ++i) {
os<< static_cast<Repr> (inp)<< ",";
}
os<< static_cast<Repr> (inp)<< "]";
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
 
P

Paul

Andrea Crotti said:
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::eek:stringstream os;
os<< "[";
size_t i;
for (i=0; i< (inp.size()-1); ++i) {
os<< static_cast<Repr> (inp)<< ",";
}
os<< static_cast<Repr> (inp)<< "]";
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 = 0xF & static_cast<T2>(vchar);
/*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 <<'\t';
}

}
 
A

Andrea Crotti

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 = 0xF & static_cast<T2>(vchar);
/*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 <<'\t';
 }

}


Thanks, but actually I don't need to convert (in this case I have
already conversions for other cases...).
I just want to use only one integer using 1 byte only, since I don't
need more.

So probably the solution of my problem could be simply to use this
typedef uint8_t hop_count_t;

and it might work already (even if now I get other errors, probably I
hardwired an int by mistake somewhere..)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top