Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Some std::string questions

Reply
Thread Tools

Some std::string questions

 
 
Nemok
Guest
Posts: n/a
 
      10-30-2005
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.

 
Reply With Quote
 
 
 
 
Joe Bacigalupa
Guest
Posts: n/a
 
      10-30-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
>



 
Reply With Quote
 
 
 
 
Nemok
Guest
Posts: n/a
 
      10-30-2005
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)

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      10-30-2005
On 30 Oct 2005 10:22:33 -0800, "Nemok" <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-30-2005
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
 
Reply With Quote
 
Nemok
Guest
Posts: n/a
 
      10-30-2005
Thanks for the usefull code. That pretty much covers it.

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      10-30-2005
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I restrict that the some ID can only login once in the some time ad ASP .Net 2 08-12-2005 09:14 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Need some help for some perl homework.... Perl 0 02-25-2004 01:45 AM
Forms Authentication question: How to have some pages open and some requiring forms authentication Eric ASP .Net 2 02-13-2004 02:14 PM
Some questions regarding 070-305 and hopefully some right answers. Needs correction... wink, wink ;-) Daniel Walzenbach MCSD 1 11-10-2003 12:25 AM



Advertisments