 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nemok Guest
|
Posted: Sun Oct 30, 2005 6:01 pm Post subject: Some std::string questions |
|
|
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.
|
|
| Back to top |
|
 |
Joe Bacigalupa Guest
|
Posted: Sun Oct 30, 2005 6:10 pm Post subject: Re: Some std::string questions |
|
|
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.prod (AT) gmail (DOT) com> wrote
| Quote: | 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.
|
|
|
| Back to top |
|
 |
Nemok Guest
|
Posted: Sun Oct 30, 2005 6:22 pm Post subject: Re: Some std::string questions |
|
|
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)
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Sun Oct 30, 2005 6:47 pm Post subject: Re: Some std::string questions |
|
|
On 30 Oct 2005 10:22:33 -0800, "Nemok" <nk.prod (AT) gmail (DOT) com> wrote:
| Quote: | 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
[email]NoSpamPlease (AT) Home (DOT) com[/email]
|
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Sun Oct 30, 2005 7:09 pm Post subject: Re: Some std::string questions |
|
|
Nemok wrote:
| Quote: | 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 );
}
| Quote: | 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
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
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
|
|
| Back to top |
|
 |
Nemok Guest
|
Posted: Sun Oct 30, 2005 8:09 pm Post subject: Re: Some std::string questions |
|
|
Thanks for the usefull code. That pretty much covers it.
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Sun Oct 30, 2005 9:03 pm Post subject: Re: Some std::string questions |
|
|
Nemok wrote:
| Quote: | 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;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|