set<string> find

This is a discussion on set<string> find within the C++ Programming forums, part of the General Programming Boards category; hi, I am new to c++ - there may be a better way to do this but I have a ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    set<string> find

    hi,
    I am new to c++ -

    there may be a better way to do this but I have a CSV file with column names on top and row names along the left side. and the matrix contains double values.

    i currently use a ifstream to pull in data into 3 containers -
    list of column strings, list of row strings, vector of doubles.

    I want to do a search on the strings in the columns to return the column position, then search the list of row strings to get the row position, then pull the value from my vector of doubles.
    I then convert the vector of doubles into a 2d matrix.

    Is this the best way to find the value from the table?
    Also, efficiency is very important.
    If it is significantly faster to convert the row strings and column strings to integers and then do the search, then that may be possible.
    Last edited by lawrenced; 07-21-2009 at 02:41 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    std::map< string, int > to map from strings to row/column indices. You then look up via:

    Code:
    double value = data[ rowMap[ rowName ] * colMap.size() + colMap[ colName ] ];
    But if "efficiency" is what you want, indexing rows and columns by name is a dumb idea. Apply a single transformation to the data set up-front which maps the strings to indices. It's kind of ridiculous to perform that lookup every single time you access the matrix.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    transform the strings into indices? is this convert the position into an integer and then use that integer wherever i would've had the string before?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by lawrenced View Post
    transform the strings into indices? is this convert the position into an integer and then use that integer wherever i would've had the string before?
    Yes, but do this only once up front, not every time you access a matrix element. Of course, if the strings are being generated dynamically and handed to you, you can't do that. But if you're given a fixed data set and row/col string pairs up front, just convert everything once in the beginning.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21