Thread: string replace and erase/insert

  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
    310
    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
    28,413
    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");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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