CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2000
    Posts
    129

    std::map & std::string

    I have a string that I need to replace key text with other text inside the string.

    For example a I have a std::string with "There were 10 hours of work done over the past 18 weeks for the customer." I would like to change 10 to ten, 18 to eighteen, and customer to Something.com.

    Currently I have a map with string pairs. One the text to search for and one for the replacement word.

    My questions are, is using a std::map the best container for this? What is the fastest way (access time speed is the most important thing) to replace the text in the string? Token the string then search the map? Loop through the map and do a replace on the string?

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    This isn't really what std::map was designed for, but you could use it.

    My question: Why not use something already tested and proven that does this? All you need is a global replace, search for the sentence and replace it with the new sentence.

    If everything is dynamic, then I'd suggest reading in 1 sentence at a time in each file, examine it and if it has digits in the string, change them to their english representation. Replacing customer should be relatively easy.

  3. #3
    Join Date
    Dec 2000
    Posts
    129
    There are a lot more words to be replaced, that was just a short example. The only thing that is dynamic is just the words to convert from and to. The string structure and length will change and most likely never be all that simular.

    What tested and proven method are you suggesting? I am open to what ever works.

  4. #4
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    Originally posted by Technocrat
    What tested and proven method are you suggesting? I am open to what ever works.
    Find and replace

    Many applications have this feature and some even do it accross multiple files (I think grep does it and there is a port of it in MinGW)

  5. #5
    Join Date
    Dec 2000
    Posts
    129
    Ok I see what you are driving at. That isnt going to work. I need to do this within my program, outside programs arent going to work. This has to do with a rather large amount of data that is a part of another program. It would take to long and serve no purpose to try to explain it. The example I gave you is just a real simplistic example, that is almost invalid with what the actual data will look like.

    Thanks for the suggestion but I just need to know whats the best way to accomplish this inside my program.

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    I would use a std::map initially and my guess is that it's fast enough. A global search and replace is too slow, since you would have to do it for every single instance.

    My Syntax Highlighter sort of works in the same way, but I use my own container instead of std::map. You can check it out in the link in my signature.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

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