473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Concerning string.cstr()

I may be a bit ignorant, or I haven't searched hard enough,
but how exactly is one to grab the finger character of a
string variable? The only way I know how to at the moment
looks a bit ugly, ie: string.cstr()[0]; Is there another
way I can do this? I figure if it *looks* ugly, then there
probably is a better way. And as a result, I've a tendency
to use C-style strings in C++ because I can't find a better
way of doing this, or scanning each individual letter of a
string in some occasions.

Thanks much,
Michael
Jul 19 '05 #1
3 9402
Michael Lawson wrote:
I may be a bit ignorant, or I haven't searched hard enough,
but how exactly is one to grab the finger character of a
string variable? The only way I know how to at the moment
looks a bit ugly, ie: string.cstr()[0]; Is there another
way I can do this? I figure if it *looks* ugly, then there
probably is a better way. And as a result, I've a tendency
to use C-style strings in C++ because I can't find a better
way of doing this, or scanning each individual letter of a
string in some occasions.



#include <iostream>

int main()
{

std::string str = "ABCD\n";

str[0] = 'X';

std::cout << str;
}

Jul 19 '05 #2

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
Michael Lawson wrote: <snip>

#include <iostream>

int main()
{

std::string str = "ABCD\n";

str[0] = 'X';

std::cout << str;
}


Yep. I'm ignorant. Thanks much.

...damn assumptions
-Michael
Jul 19 '05 #3
> I may be a bit ignorant, or I haven't searched hard enough,
but how exactly is one to grab the finger character of a
string variable? The only way I know how to at the moment
looks a bit ugly, ie: string.cstr()[0];
you can use the at(position) function

for(std::string::size_type i=0;i<s.size();++i)
{
cout << s.at(i);
}

or you can use the [] operator

for(std::string::size_type i=0;i<s.size();++i)
{
cout << s[i];
}

or you can use the std::string::iterator

for(std::string::iterator i=s.begin();i!=s.end();++i)
{
cout << (*i);
}

Is there another
way I can do this? I figure if it *looks* ugly, then there
probably is a better way.
you should have a look at a C++ documentation about the standard
template library (STL).
And as a result, I've a tendency
to use C-style strings in C++ because I can't find a better
way of doing this, or scanning each individual letter of a
string in some occasions.


I did the same when I was learning C++. The more you learn STL and
understand how it works, the less you will use some features of C
(stdio, callback, malloc/realloc... etc...)

Pierre
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
1
by: qazmlp | last post by:
Is there a memory leak here? Can't the memory deallocation happen automatically as the std::string object anyway is owning the memory? #include <cstring> #include <string> #include <iostream> ...
9
by: Shahriar | last post by:
Hi How can I achieve the following in VB. Lets say dim x as string x="3+2*5" I want to somehow convert this to a value, thus the result should be 13. In VFP, one could do something like...
4
by: dc15 | last post by:
For an intro to VB project I have to write a program which takes an amount of Miles, Yards, and Inches.....and converts it to metric (KM, M, and CM) when all values are entered to the input text...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
7
by: Andy C Matthews | last post by:
Hi there, I'm building an Access database and using VBA to generate Microsoft Word mailings for customers. It's all going fine so far. However, a variables named ParcelID, a ten-digit string such...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
4
by: MC felon | last post by:
what's the best way to compare two string elements? std::string cstr; if ( cstr.at(3) == cstr.at(2)) //do something or std::string cstr; if ( cstr == cstr) //do something ?
2
by: accessvbanewbie | last post by:
All I am trying to do is based on month value of date typed in 2 unbound form text boxes assign it to summer,winter or mixseason. I am getting a runtime erro 13 from the statement below. When I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.