Some std::string questions

N

Nemok

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.
 
J

Joe Bacigalupa

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
 
N

Nemok

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)
 
B

Bob Hairgrove

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.
 
K

Kai-Uwe Bux

Nemok said:
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 said:
> ( 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 said:
> ( 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
 
I

Ian

Nemok said:
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;
}
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top