By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Some std::string questions

P: n/a
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.

Oct 30 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <nk*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.

Oct 30 '05 #2

P: n/a
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)

Oct 30 '05 #3

P: n/a
On 30 Oct 2005 10:22:33 -0800, "Nemok" <nk*****@gmail.com> 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
No**********@Home.com
Oct 30 '05 #4

P: n/a
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
Oct 30 '05 #5

P: n/a
Thanks for the usefull code. That pretty much covers it.

Oct 30 '05 #6

P: n/a
Ian
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;
}
Oct 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.