Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why can't i access std::string with [] operator ?

77 views
Skip to first unread message

news.eternal-september.org

unread,
Apr 16, 2012, 6:09:53 PM4/16/12
to
Hello,

I'd like to access C++ strings the way i did in C , that is:
char foo[]="foo"; //foo[0] foo[1] ...

I read several times that [] was allowed for string access.
Unfortunately, i think i'm missing something.



#include <string>
#include <iostream>

using std::string;

int main(void)
{
string foo="foo";
string bar="bar";

string result = "and ? ";
result += foo[1] + bar[2];
std::cout << result << "\n"; //result should be "or"

return 0;
}


Thank you for reading.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Daniel Krügler

unread,
Apr 17, 2012, 2:35:13 AM4/17/12
to
Am 17.04.2012 00:09, schrieb news.eternal-september.org:
> Hello,
>
> I'd like to access C++ strings the way i did in C , that is:
> char foo[]="foo"; //foo[0] foo[1] ...
>
> I read several times that [] was allowed for string access.

It is allowed, see 21.4 [basic.string]:

// 21.4.5, element access:
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);

Note that (const_)reference are just alias for a reference to char
(const).

> Unfortunately, i think i'm missing something.
>
> #include <string>
> #include <iostream>
>
> using std::string;
>
> int main(void)
> {
> string foo="foo";
> string bar="bar";
>
> string result = "and ? ";
> result += foo[1] + bar[2];

Nice "gotcha" example for operator overloading: The language is
designed such to allow extending it, but not changing it. Note that
the expressions foo[1] and bar[2] just have type char, so before the
compound assignment operator += takes action, we simply have the
effect of adding the values of two chars. The result is the ordinal
value of adding 'o' and 'r'. The character value of this sum will then
be added as trailing new character to the string "and ? "

The right fix in this example is to split the assignment into two
steps:

result += foo[1];
result += bar[2];

HTH & Greetings from Bremen,

Daniel Krügler

red floyd

unread,
Apr 17, 2012, 2:37:04 AM4/17/12
to
On 4/16/2012 3:09 PM, news.eternal-september.org wrote:
> Hello,
>
> I'd like to access C++ strings the way i did in C , that is:
> char foo[]="foo"; //foo[0] foo[1] ...
>
> I read several times that [] was allowed for string access.
> Unfortunately, i think i'm missing something.

You are. Operator precedence.
>
>
>
> #include <string>
> #include <iostream>
>
> using std::string;
>
> int main(void)
> {
> string foo="foo";
> string bar="bar";
>
> string result = "and ? ";
> result += foo[1] + bar[2];
Equivalent to: result += (foo[1] + bar[2]);

Hope that helps.
> std::cout << result << "\n"; //result should be "or"
>
> return 0;
> }
>


Edward Diener

unread,
Apr 17, 2012, 2:44:09 AM4/17/12
to
{ Please restrict your quoting to the minimum needed to
establish context -mod/we }

On 4/16/2012 6:09 PM, news.eternal-september.org wrote:
> Hello,
>
> I'd like to access C++ strings the way i did in C , that is:
> char foo[]="foo"; //foo[0] foo[1] ...
>
> I read several times that [] was allowed for string access.
> Unfortunately, i think i'm missing something.
>
>
>
> #include <string>
> #include <iostream>
>
> using std::string;
>
> int main(void)
> {
> string foo="foo";
> string bar="bar";
>
> string result = "and ? ";
> result += foo[1] + bar[2];
> std::cout << result << "\n"; //result should be "or"
>
> return 0;
> }
>
>
> Thank you for reading.
>
>

Why do you think that foo[i] + bar[2] becomes "or" ?

acray

unread,
Apr 17, 2012, 4:31:55 PM4/17/12
to
When you do this:

foo[1] + bar[2]

you are computing the sum of two chars, you are not concatenating two
characters into a string.
Your problem has nothing to do with string [] operator.

On 17 Apr, 00:09, "news.eternal-september.org"
<jseb_usenet2...@finiderire.com> wrote:
> Hello,
>
> I'd like to access C++ strings the way i did in C , that is:
> char foo[]="foo"; //foo[0] foo[1] ...
>
> I read several times that [] was allowed for string access.
> Unfortunately, i think i'm missing something.
>
> #include <string>
> #include <iostream>
>
> using std::string;
>
> int main(void)
> {
> string foo="foo";
> string bar="bar";
>
> string result = "and ? ";
> result += foo[1] + bar[2];
> std::cout << result << "\n"; //result should be "or"
>
> return 0;
>
> }

{ Quoted banner removed -mod }

Ulrich Eckhardt

unread,
Apr 17, 2012, 4:37:48 PM4/17/12
to
Am 17.04.2012 00:09, schrieb news.eternal-september.org:
> I'd like to access C++ strings the way i did in C

C strings are still available in C++ if you need that, but I don't think
you do.


> string foo="foo";
> string bar="bar";
>
> foo[1] + bar[2];

The problem is this expression. "foo[1]" yields a char, "bar[2]", too.
Adding those two does not create a string, it just treats those two
chars as integers. This is different from e.g. Python, where these
expressions yield strings of size 1, but it's the same as in C.

What you can do is use one of the substr() member function overloads.
Another way is that you avoid the + operation on chars, and use two +=
operations on the string instead. You could also explicitly convert (at
least) one of the two chars to a string, which would then trigger a
different operator+ overload.


Good luck!

Uli
0 new messages