Concerning string.cstr()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Lawson

    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


  • Gianni Mariani

    #2
    Re: Concerning string.cstr()

    Michael Lawson wrote:[color=blue]
    > 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.[/color]




    #include <iostream>

    int main()
    {

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

    str[0] = 'X';

    std::cout << str;
    }

    Comment

    • Michael Lawson

      #3
      Re: Concerning string.cstr()


      "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
      news:bnkqro$m9p @dispatch.conce ntric.net...[color=blue]
      > Michael Lawson wrote:[/color]
      <snip>[color=blue]
      >
      >
      > #include <iostream>
      >
      > int main()
      > {
      >
      > std::string str = "ABCD\n";
      >
      > str[0] = 'X';
      >
      > std::cout << str;
      > }
      >[/color]

      Yep. I'm ignorant. Thanks much.

      ...damn assumptions
      -Michael


      Comment

      • Pierre

        #4
        Re: Concerning string.cstr()

        > I may be a bit ignorant, or I haven't searched hard enough,[color=blue]
        > 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];[/color]

        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::it erator

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

        [color=blue]
        > Is there another
        > way I can do this? I figure if it *looks* ugly, then there
        > probably is a better way.[/color]

        you should have a look at a C++ documentation about the standard
        template library (STL).
        [color=blue]
        > 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.[/color]

        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

        Comment

        Working...