Some std::string questions

Discussion in 'C++' started by Nemok, Oct 30, 2005.

  1. Nemok

    Nemok Guest

    Hi,

    I am new to STD so I have some questions about std::string because I
    want use it in one of my projects instead of CString.

    1. Is memory set dinamicaly (like CString), can I define for example
    string str1; as a class member and then add text to it. or do I have
    to
    specify it's length when defining?
    2. How to convert from std::string to LPCSTR
    3. How can I deallocate a std::string var, to free memory after I am
    done
    with it?
    4. How to convert a std::string to integer
    5. How to set the value of a std::string to integer

    That's about it for now but there is lot more to come.
    Thanks.
    Nemok, Oct 30, 2005
    #1
    1. Advertising

  2. 1.std::string is dynamic, you can grow it and shrink it whenever you need
    to.
    2. the std::string function .c_str()
    3.it's destructor does that for you
    4/5. look into using std::stringstream to convert to and from integer/float
    and string types

    hope that helps,
    Joe

    "Nemok" <> wrote in message
    news:...
    > Hi,
    >
    > I am new to STD so I have some questions about std::string because I
    > want use it in one of my projects instead of CString.
    >
    > 1. Is memory set dinamicaly (like CString), can I define for example
    > string str1; as a class member and then add text to it. or do I have
    > to
    > specify it's length when defining?
    > 2. How to convert from std::string to LPCSTR
    > 3. How can I deallocate a std::string var, to free memory after I am
    > done
    > with it?
    > 4. How to convert a std::string to integer
    > 5. How to set the value of a std::string to integer
    >
    > That's about it for now but there is lot more to come.
    > Thanks.
    >
    Joe Bacigalupa, Oct 30, 2005
    #2
    1. Advertising

  3. Nemok

    Nemok Guest

    Thanks for your answers.

    Something more:
    1. How can I use std::stringstream to convert to and from integer/float
    and string types ? I searched for it but found nothing usefull.
    2. How can I convert characters in a std::string to low characters.
    (like MakeLower() in CString)
    Nemok, Oct 30, 2005
    #3
  4. On 30 Oct 2005 10:22:33 -0800, "Nemok" <> wrote:

    >Thanks for your answers.
    >
    >Something more:
    >1. How can I use std::stringstream to convert to and from integer/float
    >and string types ? I searched for it but found nothing usefull.
    >2. How can I convert characters in a std::string to low characters.
    >(like MakeLower() in CString)


    1. How about this? (untested code follows...)

    template<typename NumericType>
    typename NumericType ToNumber<NumericType>(
    std::string const &s) {
    NumericType retval;
    std::istringstream iss(s);
    iss >> retval;
    return retval;
    }

    Obviously, this is NOT robust code ... but it may well serve your
    purpose. In particular, it doesn't do any sanity checking WRT bounds
    checking, no exception safety, etc.

    2. I would use the CRT functions (tolower(), toupper(), etc.) but only
    if you do not need any i18n support ... otherwise, you need to look
    into locales and facets, especially codecvt.

    --
    Bob Hairgrove
    Bob Hairgrove, Oct 30, 2005
    #4
  5. Nemok

    Kai-Uwe Bux Guest

    Nemok wrote:

    > Thanks for your answers.
    >
    > Something more:
    > 1. How can I use std::stringstream to convert to and from integer/float
    > and string types ? I searched for it but found nothing usefull.


    e.g.:

    template< typename T >
    bool convert_to_string ( std::string & str, const T & obj ) {
    std::stringstream dummy;
    bool result = ( dummy << obj );
    if ( result ) {
    str = dummy.str();
    }
    return( result );
    }

    template< typename T >
    bool convert_from_string ( T & obj, const std::string & str ) {
    std::stringstream dummy ( str );
    bool result = ( dummy >> obj );
    return( result );
    }

    > 2. How can I convert characters in a std::string to low characters.
    > (like MakeLower() in CString)


    e.g:

    #include <locale>
    #include <string>
    #include <iostream>
    #include <functional>
    #include <algorithm>

    template < typename CharT >
    class to_lower {

    std::locale const & loc;

    public:

    to_lower ( std::locale const & r_loc = std::locale() )
    : loc ( r_loc )
    {}

    CharT operator() ( CharT chr ) const {
    return( std::tolower( chr, this->loc ) );
    }

    }; // class to_lower;

    template < typename CharT >
    class to_upper {

    std::locale const & loc;

    public:

    to_lower ( std::locale const & r_loc = std::locale() )
    : loc ( r_loc )
    {}

    CharT operator() ( CharT chr ) const {
    return( std::toupper( chr, this->loc ) );
    }

    }; // class to_upper;


    template < typename CharIter >
    void make_lower ( CharIter from, CharIter to,
    std::locale const & loc = std::locale() ) {
    std::transform( from, to, from,
    to_lower<
    typename std::iterator_traits< CharIter >::value_type
    > ( loc ) );

    }

    template < typename CharIter >
    void make_upper ( CharIter from, CharIter to,
    std::locale const & loc = std::locale() ) {
    std::transform( from, to, from,
    to_upper<
    typename std::iterator_traits< CharIter >::value_type
    > ( loc ) );

    }

    template < typename CharT >
    std::basic_string< CharT > make_lower ( std::basic_string< CharT > & str,
    std::locale const & loc = std::locale() ) {
    std::basic_string< CharT > result = str;
    make_lower( result.begin(), result.end(), loc );
    return( result );
    }

    template < typename CharIter >
    bool
    sequence_equal_to_ignoring_case ( CharIter a_from, CharIter a_to,
    CharIter b_from, CharIter b_to,
    std::locale const & loc = std::locale() ) {
    if ( std::distance( a_from, a_to )
    !=
    std::distance( b_from, b_to ) ) {
    return ( false );
    }
    for ( CharIter a_iter = a_from, b_iter = b_from;
    a_iter != a_to;
    ++ a_iter, ++b_iter ) {
    if ( to_lower( *a_iter, loc ) != to_lower( *b_iter, loc ) ) {
    return ( false );
    }
    }
    return ( true );
    }

    bool
    string_equal_to_ignoring_case ( std::string const & a,
    std::string const & b,
    std::locale const & loc = std::locale() ) {
    return( sequence_equal_to_ignoring_case( a.begin(), a.end(),
    b.begin(), b.end(),
    loc ) );
    }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 30, 2005
    #5
  6. Nemok

    Nemok Guest

    Thanks for the usefull code. That pretty much covers it.
    Nemok, Oct 30, 2005
    #6
  7. Nemok

    Ian Guest

    Nemok wrote:
    > 2. How can I convert characters in a std::string to low characters.
    > (like MakeLower() in CString)
    >

    The most basic form:

    #include <string>
    #include <iostream>
    #include <algorithm>

    int
    main()
    {
    std::string s("aBcDeFg");

    std::transform( s.begin(), s.end(), s.begin(), std::tolower );

    std::cout << s << std::endl;
    }
    Ian, Oct 30, 2005
    #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.

Share This Page