CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Question Delete_items_from_string

    Hi,

    Is it possible to delete certain items from a char sequence or string?
    Say i have:

    char test[5] = {'a','b','c','d','e'};

    Is it possible to delete 'a' for example?

    Thanks!

    - Neil.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Delete_items_from_string

    What you would need to do in that case is shuffle b, c, d, and e over one space, and then mark the array as being one shorter.

    This is in general why erasing an element from an array is O(n). Other data structures are better for frequent erasures at arbitrary positions.

    Of course, erasing an element from the *end* of an array is O(1), since you only need to reduce the apparent array size by 1.

  3. #3
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Re: Delete_items_from_string

    Thanks for your reply, I understand. Would using a std::string in this case be more efficient (as in faster to make)?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Delete_items_from_string

    Using a std::string is almost always better than using a plain char array. However, erasing an arbitrary character isn't going to be any faster, because it's doing the same thing under the hood.

    Perhaps if you explain your problem at a high level, an appropriate data structure could be suggested.

  5. #5
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Re: Delete_items_from_string

    Sure, here's the code. Its a program that reads a file. The first to lines are int, first int states how many rows there are after the 2 lines that have been read. The second int says how much colums there are.
    Each cell in the matrix is a char (one letter of the alphabet).
    I need to output the letters of the alfabet that haven't been used. To do this i want to create a string list with the alfabet, then for each char read, check if its in the list, then if it is, remove it from the list. Hopefully this makes sense, here's the code:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {
    ifstream diagram;
    diagram.open("diagram.in");
    char alfabet[26] = ;
    string line;

    getline(diagram,line);
    int one = atoi(line.c_str());
    getline(diagram,line);
    int two = atoi(line.c_str());

    for(int i=0;i<=two;i++)
    {
    getline(diagram, line);
    for (int n=0;n<=two;n++)
    {
    char toremove = line[n];
    // here the program has to check wheter the char is inside alfabet, and if it is delete it
    }
    }
    // output remaining characters from alfabet
    return 0;
    }

    - Neil

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Delete_items_from_string

    I would suggest using a bool[26] array, initialized to false, and just set the corresponding index to true whenever you encounter a particular letter. One way to get the index would be
    Code:
    index = tolower(c) - 'a';
    Alternatively, you could do something akin to your current plan but more efficient if you use a std::set<char> rather than an array, since erasing an element from a set is O(log n).

  7. #7
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Re: Delete_items_from_string

    Thanks Lindley!

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Delete_items_from_string

    Actually, to keep into the spirit of the original design, a std::set<char> would be perfect. It is the perfect structure, as described by your need.

    Now, if you reword your problem, Lindley's suggestion is better, but if just keeping in mind with finding letters and removing them, a set is an excellent structure.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured