string replace and erase/insert

This is a discussion on string replace and erase/insert within the C++ Programming forums, part of the General Programming Boards category; first of all, is it possible to do an array of strings? Either that or an array of character sequences ...

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    string replace and erase/insert

    first of all, is it possible to do an array of strings? Either that or an array of character sequences (if that's the same thing).

    Code:
    string start0 = (" _____________ ");
    string start1 = ("|   ___       |");
    string start2 = ("|  |_ _|      |");
    string start3 = ("|             |");
    string start4 = ("|       | |   |");
    string start5 = ("|_______| |___|");
    
    string  *hello[5];
    hello[0] = &start0;
    hello[1] = &start1;
    hello[2] = &start2;
    hello[3] = &start3;
    hello[4] = &start4;
    hello[5] = &start5;
    
    
    *hello[y].replace (x,1, "X" );
    ^where the variable y and x are input by the user. It wont let me use a pointer there. Is there any other alternative?

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    How about
    hello[y]->replace(...)
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    is it possible to do an array of strings?
    It is possible to have an array of strings. However, you may find it better to have a std::vector<std::string>

    Code:
    // create a vector of 6 default initialised strings
    std::vector<std::string> start(6);
    // populate the strings in the vector with the desired values
    start[0] = " _____________ ";
    start[1] = "|   ___       |";
    start[2] = "|  |_ _|      |";
    start[3] = "|             |";
    start[4] = "|       | |   |";
    start[5] = "|_______| |___|";
    ^where the variable y and x are input by the user. It wont let me use a pointer there. Is there any other alternative?
    It depends on what exactly you want to do, but with this vector of strings you may be able to write:
    Code:
    start[y].replace(x, 1, "X");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    thanks a lot guys, i really appreciate it

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21