Thread: Array of Structs question

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Array of Structs question

    Ok, after using Thantos suggestion of using an array of structs I have managed to build my struct and populate it in simple manner using integers to index my array.

    However, I need to index my array using the value of a string. Does anyone know how I might use an array of strings tied to my struct?

    the first successful aspec I used with ints was like this:

    Code:
    struct mystruct
    {
       char id [11];
       int station;
       int wave;
    }
    
    mystruct mystructList [20];
    
    strcpy(mystructList[wave].charid, someid);
    mystructList[wave].station =1;
    mystructList[wave].wave = 1;
    when I use [wave] this represents the value between 0-19 that I want to address where my struct info will be added. The problem is... I need to load an array of strings and index the location of struct according to a specific string.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    First of all this is C++, not C so you should be using a vector of classes not an array of structs. Here is a link to the STL http://www.sgi.com/tech/stl/ where you can look up everything you need about those. Although I am not sure why you would want to index by name in your case, the way to accomplish that is to use a hash set. Again you can look up all relevant information for that at the above link.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Correct me if I am wrong but I think an array of structs is completely legal in C++.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    SHOULD be using, not have to be using. Arrays and structs are perfectly legal in C++, but so are goto statements. The point is that if you are not using classes and vectors you are not utilizing the language properly.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    structs ARE classes. Only difference between them is that structs are default public while classes are default private. As for vectors it might be a little more the OP wants to bite off atm. Also based off of
    However, I need to index my array using the value of a string.
    I would say a map would be a better choice then a vector.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    The reason for arrays and structs are because of the extreme amount of data and the speed at which it must be referenced. Since arrays and structs are loaded and referenced in RAM, it makes my program searches much faster.

    I don't have a vast enough knowledge of vectors so I can not compare, but this is my reasoning for the arrays of structs. I will look at the vector link though, as I am sure I need to know this. Thanks

    I did however solve my problem, I simply built and array to load my table of employees and then create a binary search using strcmp to resolve the location in that table, then I feed this to my array of struct and then use that location from the search to index the array of struct. Very fast and solves my issue of using multi dem. arrays.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    164
    I agree Thantos, I am learning though, and everyones advice is helping. Thanks to all. Thantos, your orignal idea of the array of structs has been very helpful.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    Quote Originally Posted by WaterNut
    ...

    I did however solve my problem, I simply built and array to load my table of employees and then create a binary search using strcmp to resolve the location in that table, then I feed this to my array of struct and then use that location from the search to index the array of struct. Very fast and solves my issue of using multi dem. arrays.
    What you just described is already part of the STL. It is a hash map although it is implemented differently. Binary searches and many other useful algorithms are also part of the STL. Additionally your worries about speed are very unjustified. In most cases the STL is just as fast or faster than what you have hacked together.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you just described is already part of the STL. It is a hash map although it is implemented differently. Binary searches and many other useful algorithms are also part of the STL. Additionally your worries about speed are very unjustified. In most cases the STL is just as fast or faster than what you have hacked together.
    Ah but doing it your self gives you much more knowledge and experience then just using the STL from the beginning.

    While I use std::list a lot I still created my own (over and over) until I understood how they worked, their strengths, and their weaknesses

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    You have a very valid point. Since he asked how to do it I simply told him the best way. I think it is important both to learn to use the STL and understand it.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    164
    very valid on both sides, thats why I'm here! I will definately study the use of STL for future reference though.

    This has been a great learning experience on arrays, structs and how to use the likes of...

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 03:04 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM