vector representation

Discussion in 'C++' started by Andrea Crotti, Apr 9, 2011.

  1. 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
     
    Andrea Crotti, Apr 9, 2011
    #1
    1. Advertising

  2. Andrea Crotti

    Ian Collins Guest

    On 04/ 9/11 11:11 PM, Andrea Crotti wrote:
    > 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]

    --
    Ian Collins
     
    Ian Collins, Apr 9, 2011
    #2
    1. Advertising

  3. Andrea Crotti

    Paul Guest

    "Andrea Crotti" <> wrote in message
    news:...
    >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
    >
     
    Paul, Apr 9, 2011
    #3
  4. On 9 Apr, 13:45, Ian Collins <> wrote:
    > 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..
     
    Andrea Crotti, Apr 9, 2011
    #4
  5. 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
     
    Andrea Crotti, Apr 9, 2011
    #5
  6. Andrea Crotti

    Paul Guest

    "Andrea Crotti" <> wrote in message
    news:...
    > 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';
    }

    }
     
    Paul, Apr 9, 2011
    #6
  7. On Apr 9, 4:53 pm, "Paul" <> wrote:
    >
    > 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..)
     
    Andrea Crotti, Apr 10, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,969
  2. Replies:
    8
    Views:
    1,970
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
  4. barcaroller
    Replies:
    7
    Views:
    456
    barcaroller
    Nov 8, 2008
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    379
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page