473,769 Members | 3,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Oct 30 '05 #1
6 11509
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::stringstre am to convert to and from integer/float
and string types

hope that helps,
Joe

"Nemok" <nk*****@gmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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
Thanks for your answers.

Something more:
1. How can I use std::stringstre am 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
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::stringstre am 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<typena me NumericType>
typename NumericType ToNumber<Numeri cType>(
std::string const &s) {
NumericType retval;
std::istringstr eam 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**********@Ho me.com
Oct 30 '05 #4
Nemok wrote:
Thanks for your answers.

Something more:
1. How can I use std::stringstre am 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_stri ng ( std::string & str, const T & obj ) {
std::stringstre am dummy;
bool result = ( dummy << obj );
if ( result ) {
str = dummy.str();
}
return( result );
}

template< typename T >
bool convert_from_st ring ( T & obj, const std::string & str ) {
std::stringstre am 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_t raits< 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_t raits< CharIter >::value_type ( loc ) );

}

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

template < typename CharIter >
bool
sequence_equal_ to_ignoring_cas e ( 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_cas e( a.begin(), a.end(),
b.begin(), b.end(),
loc ) );
}

Best

Kai-Uwe Bux
Oct 30 '05 #5
Thanks for the usefull code. That pretty much covers it.

Oct 30 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3662
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13333
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
19
6164
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
9
3301
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
12
13592
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
14
12195
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++ books (Effective C++, More Effective C++, Exceptional C++, More Exceptional C++, C++ FAQs, Addison Wesley 2nd Edition) Authors of these books have not considered UNICODE. So many of their
2
5511
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
15899
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
29
23219
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.