[RESOLVED] Need advice - what to use CStringArray or CStringList
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 44

Thread: [RESOLVED] Need advice - what to use CStringArray or CStringList

  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    [RESOLVED] Need advice - what to use CStringArray or CStringList

    I have a program that first counts and remembers file paths in a specified folder in accordance with a certain filter and then processes them. The counting part of the function needs to collect all file names into an array. The processing part needs to see if a certain file name is in that array.

    What type of arrays shall I use for that: CStringArray, CStringList or CStringMap? I'm kind of confused over what each of them does (please don't point me to a Microsoft description, I read it about five times and I still don't get it.) I'm basically looking for the fastest implementation of the dynamic array for this purpose.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: Need advice - what to use CStringArray or CStringList

    Are you interested in the fasted insertion or fastest lookup?

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need advice - what to use CStringArray or CStringList

    I'll technically be using both.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Need advice - what to use CStringArray or CStringList

    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by ahmd View Post
    I'll technically be using both.
    Yes, of course, but which is going to be more important? Once collected, are you going to do lots of look-ups? I would consider using a map in this case.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by cilu View Post
    Yes, of course, but which is going to be more important? Once collected, are you going to do lots of look-ups? I would consider using a map in this case.
    I agree. If you need to do a lot of lookups based on a value, a map would be best.

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Need advice - what to use CStringArray or CStringList

    If number of items are only inserted at end, and around 100 elements reside in container, then std::vector is very appropriate. Even with 1000 elements, vector search works faster than maps!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need advice - what to use CStringArray or CStringList

    I'm searching recursively thru files in a folder (so I can't tell you how many there are -- it may be anything from 0 to 10,000 and more). At this stage I need to collect all the file paths in an array.

    When all files are found, the second round of recursive search opens each file and processes it in a certain way. At this point if a certain tag is found in a file, I need to match that tag with a file name in the array created above. Each file, on average, may contain from 0 to 10 of those tags, but that number can be more.

    So what would you use in this case? (I can't go with std::vector since I've never used it before and I need to go with something that I already know.)

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,625

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by ahmd View Post
    When all files are found, the second round of recursive search opens each file and processes it in a certain way. At this point if a certain tag is found in a file, I need to match that tag with a file name in the array created above. Each file, on average, may contain from 0 to 10 of those tags, but that number can be more.
    Could you explain this in more detail ?

    1) What is recursive about this search ?

    2) What do you ultimately want :

    a) for each file ... a list of tags in that file ?

    b) for each tag ... a list of files that have that tag ?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by ahmd View Post
    I'm searching recursively thru files in a folder (so I can't tell you how many there are -- it may be anything from 0 to 10,000 and more). At this stage I need to collect all the file paths in an array.

    When all files are found, the second round of recursive search opens each file and processes it in a certain way. At this point if a certain tag is found in a file, I need to match that tag with a file name in the array created above. Each file, on average, may contain from 0 to 10 of those tags, but that number can be more.

    So what would you use in this case? (I can't go with std::vector since I've never used it before and I need to go with something that I already know.)
    Why a second recursive search? You already have all your file names in your collection object, why not just iterate through that?

  11. #11
    Join Date
    Jun 2006
    Posts
    644

    Re: Need advice - what to use CStringArray or CStringList

    Just 2 cents...
    I hope ahmd refers to a method that searches for all the files in a folder, gets / stores their paths and if a folder is encountered, it calls itself (recursion). So instead of storing only the paths, you can also store the information regarding the parent and the level (starting from 0 for the starting directory). You could just create a class that holds a files path (CString), parent (CString) and level (int) and then create a vector of that class?
    BTW, using a vector is very simple...if you are not comfortable with STL, you are better off using CArray provided by MFC or even better if you use CAtlArray, if you do not need the serialization support provided by MFCs CObject.
    Bhushan

  12. #12
    Join Date
    Jun 2006
    Posts
    644

    Re: Need advice - what to use CStringArray or CStringList

    As I said, here is a MFC code to do the find:
    You can still get the path, parent and level info and store it in a class and create its MFC Array as I said before...

    Bhushan

  13. #13
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by Philip Nicoletti View Post
    1) What is recursive about this search ?
    Pretty much what Bhushan showed in his MSDN link -- if I find a folder the function calls itself with it's path.

    Quote Originally Posted by Philip Nicoletti View Post
    a) for each file ... a list of tags in that file ?
    No.

    Quote Originally Posted by Philip Nicoletti View Post
    b) for each tag ... a list of files that have that tag ?
    If I find that the tag matches I'm updating the file that this tag is in and save it back to disc.

    Quote Originally Posted by GCDEF View Post
    Why a second recursive search? You already have all your file names in your collection object, why not just iterate through that?
    Good point. The reason I do this is because it's an option that can be switched off in the program. Without this option being on, I need to count all the files first to display the progress bar during the second pass.

    Quote Originally Posted by bhushan1980 View Post
    So instead of storing only the paths, you can also store the information regarding the parent and the level (starting from 0 for the starting directory). You could just create a class that holds a files path (CString), parent (CString) and level (int) and then create a vector of that class?
    I have the code that's already tested and working, rewriting it completely is not what I'm looking for at this point. (Again my goal is to speed it up/optimize it.) But thanks for offering. Truly I didn't think about such approach. Although, wouldn't it be much slower if I propagate the whole class instead of just a CString object?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by ahmd View Post

    Good point. The reason I do this is because it's an option that can be switched off in the program. Without this option being on, I need to count all the files first to display the progress bar during the second pass.
    Okay, but what I'm saying is you can get all the paths from your container for the second pass. No need to go searching the whole directory structure again. That should be high on your list of things to eliminate if you're concerned about performance.

  15. #15
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need advice - what to use CStringArray or CStringList

    Quote Originally Posted by GCDEF View Post
    Okay, but what I'm saying is you can get all the paths from your container for the second pass. No need to go searching the whole directory structure again. That should be high on your list of things to eliminate if you're concerned about performance.
    Well, not really. I actually started from your approach and what I observed was that once the number of files gets over, lets say, a thousand, it takes longer to collect all file paths in a memory array and then use them from there, than simply doing two file search passes. What happens is that first it goes really fast but then it literally slows down to a crawl (probably due to constant memory reallocations of the dynamic array.) The file search, I believe is cached by the OS so the second pass runs very fast.

Page 1 of 3 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center