Thread: problem with deletion of a map element and insertion of a new element in its place.

  1. #1
    Registered User
    Join Date
    Aug 2021
    Posts
    2

    Question problem with deletion of a map element and insertion of a new element in its place.

    Hello everyone.
    This question is from the book "Jumping into C++", chapter 19. I have removed "<" from HTML tags, so that the whole question is visible.

    Write a program that reads in HTML text that the user types in (don’t worry, we’ll cover how to
    read from a file later). It should support the following HTML tags: html>, head>, body>, b>,
    i>, and a>. Each HTML tag has an open tag, e.g. html>, and a closing tag which has a forwardslash
    at the start: /html>. Inside the tag is text that is controlled by that tag: b>text to be
    bolded/b> or i>text to be italicized/i>. The head> /head> tags control text that is
    metadata, and the body> /body> tags surround text that is to be displayed. a> tags are used
    for hyperlinks, and have an URL in the following format: <a href=URLtext /a>.
    Once your program has read in some HTML, it should simply ignore html>. It should remove
    any text from the head> section so that it doesn't show up when you output it. It should then
    display all text in the body, modifying it so that any text between b> and /b> will show up
    with asterisks (*) around it, any text inside i> and /i> will show up with underscores (_)
    around it, and any text with a <a href=linkurl link text /a> tag shows up as link text (linkurl).

    --------------------------------------------------------------------------------------------------
    Brief explanation of my attempt -
    Step 1 - assigned the string between body tags to a new string variable "body".
    Step 2 - created a map "check" and inserted parts of the string in "body" leaving out the string between b> /b> tags.
    Step 3 - tried to replaced the inserted elements of map "check" which contain i> i/> tags, by first assigning the values of itr->first and itr->second to new variables, then deleting that element and inserting new elements in map after removing string between i> /i> tags.

    **This is where I face the problem. The code doesn't give an error but the program doesn't return 0. If I remove the "check.erase(index)", it works, but that doesn't solve the purpose.**

    I tried doing a similar thing in a separate code and it worked. I have included the other code after the following one, which is the attempted solution to the question above.

    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 int main() { map<int,string>check; string x = "<html>"; string xx = "</html>"; string y = "<head>"; string yy = "</head>"; string z = "<body>"; string zz = "</body>"; string a = "<b>"; string aa = "</b>"; string b = "<i>"; string bb = "</i>"; string c = "<a href"; string ccc = ">"; string cc = "</a>"; string type = "<html>\n<head>I dont know what to write here</head>\n<body>\ngurasees is <b>my</b> good <i>name</i>. You can find <b>me</b> on the <i>web</i>. Link is <a href = www.google.com>gura</a>\n</body>\n</html>"; int f = type.find(z)+z.size()+1; int g = type.find(zz)-f; string body = type.substr(f,g); map<int,string>::iterator itr; map<int,string>::iterator ends = check.end(); //--------------------------STEP 2----------------------------------------- check.insert ({body.find(body[0]), body.substr (body.find(body[0]), body.find(a,0)-1)}); int k = body.find(a, 0); k++; int p = body.find(aa,0); for (int i = body.find(a,k); i != string::npos && p != string::npos; i = body.find(a, i), p = body.find(aa, p)) { check.insert({p+aa.size(),body.substr(p+aa.size(), i-1-(p+aa.size()))}); i++; p++; } check.insert({p + aa.size() , body.substr(p + aa.size() , body.size() - 1 - p ) } ) ; //------------------------STEP 3------------------------------------------- for (itr = check.begin(); itr != ends; itr++) { cout << itr->second.find(b) << endl; //prints out correctly if (itr->second.find(b) != string::npos) { int index = itr->first; string italic_component = itr->second; check.erase(index); check.insert ({index, italic_component.substr(italic_component.find(italic_component[0]), italic_component.find(b, 0)-1)}); int k = italic_component.find(b, 0); k++; int j = italic_component.find(bb, 0); for (int i = italic_component.find(b, k); i != string::npos && j != string::npos; i = italic_component.find(b, k), j = italic_component.find(bb, j) ) { check.insert({index + j + bb.size() , italic_component.substr(j + bb.size(), i - 1 - (j+bb.size()))}); i++; j++ ; } check.insert({j + index + bb.size() ,italic_component.substr(j + bb.size() , italic_component.size() - 1 - j ) } ) ; } } //------------------------------STEP 4------------------------------------- for ( int i = body.find( a , 0 ), j = body.find( aa , 0), k = body.find( b , 0 ), l = body.find( bb , 0); i != string::npos && j != string::npos && j != string::npos && k != string::npos; i = body.find(a, i ), j = body.find(aa,j), k = body.find(b, k), l = body.find(bb,l) ) { check.insert( {i, "*" + body.substr(i+a.size(), j-(i+a.size())) + "*" } ); check.insert( {k, "_" + body.substr(k+b.size(), l-(k+b.size())) + "_" } ); i++; j++; k++; l++; } } Edit & Run
    -------------------------THE OTHER CODE--------------------------------
    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 int main() { map<int,string>check; check[10] = "gurasees is there for you"; check[20] = "aka is not born yet"; check[30] = "sam please wait for years"; map<int,string>::iterator itr; map<int,string>::iterator ends = check.end(); for(itr = check.begin(); itr!= ends; itr++) { if(itr->second.find("born") != string::npos) { int index = itr->first; string italic = itr->second; check.erase(itr->first); check.insert({index, italic.substr(italic.find(italic[0]), 5)}); } }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Also here -> problem with deletion of a map element a - C++ Forum

    Looks like you mucked it all up by copying directly from their HTML rather than pasting a fresh text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2021
    Posts
    2
    Quote Originally Posted by Salem View Post
    Also here -> problem with deletion of a map element a - C++ Forum

    Looks like you mucked it all up by copying directly from their HTML rather than pasting a fresh text.
    I posted the question on another forum as well. So I copied it from there.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by asees View Post
    I posted the question on another forum as well. So I copied it from there.
    The point is that you didn't mention the other post in your question. Someone might have wasted their time helping you when you had already received an answer at the other site.

    BTW, it seems that my problems with "Reply with quote" (and "quick reply") were possibly related to DarkReader (browser extension).
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compare element of array with other element
    By Djsarkar in forum C Programming
    Replies: 28
    Last Post: 08-04-2020, 03:59 PM
  2. Array Element Insertion in Ascending Order Question
    By Omegalis in forum C Programming
    Replies: 1
    Last Post: 03-20-2018, 10:14 AM
  3. Getting the same element vs getting random element.
    By Haratino in forum C Programming
    Replies: 2
    Last Post: 03-01-2016, 11:09 AM
  4. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  5. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM

Tags for this Thread