Thread: How can I create an array of nodes

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    Question How can I create an array of nodes

    I am trying to see if I can create an array of nodes. I think I have it down somewhat but this is what I have.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    /*This is a template of a person....sorta*/
    class person{
    public:
    	int data[32];
    	bool selected = false;
    	person(){
    		for (int x = 0; x < 32; x++){
    			data[x] = 0;
    		}
    	}
    };
    
    
    //This is where the magic happens
    int main()
    {
    	/*Variable block**********************************/
    	int row = 86;
    	const int column = 32;
    	/*************************************************/
    
    
    
    
    	/*Creates the nodes*/
    	person *bitstring = new person[row];
    	cout << bitstring->data[31] << endl;
    
    
    
    	return 0;
    }
    I am trying to see if there is a way for me to create a 86 slot array of person nodes. I am not sure how to do it and how to access them afterwards. I know that I am creating the bitstring of person correctly but I am not sure how to get the array of person going.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By "node" did you mean "object"? Are you just trying to create an array of 86 person objects?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    2
    I want to create 86 person objects that will contain the bitstrings inside them with a boolean value for later use. I know that I can zero out the array but I don't know how to put each person object into a managable array for me to access. I want to be able to access them like, I want person 1 and person 53 or person 68 and person 65.

    Essentially, just an array of objects. I'm certain that bitstring->data[x] would tell me what is in data array at the x position and bitstring->selected would give me a true or false depending on what I set it on but I don't know which person I am accessing or if I even have more than one person.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest something like this:
    Code:
    #include <array>
    #include <bitset>
    #include <iostream>
    
    class Person
    {
    public:
        std::bitset<32> data;
        bool selected = false;
    };
    
    typedef std::array<Person, 86> PersonList;
    
    int main()
    {
        using namespace std;
    
        PersonList persons;
        cout << persons[0].data[20] << endl;
        return 0;
    }
    You would need to read up on std::bitset and std::array if you are not familiar with them, but since you are using C++11 syntax for initialising the selected member variable, I assume that you will have no problem with std::array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by skynet0928 View Post
    Essentially, just an array of objects. I'm certain that bitstring->data[x] would tell me what is in data array at the x position and bitstring->selected would give me a true or false depending on what I set it on but I don't know which person I am accessing or if I even have more than one person.
    Not quite (but almost). The variable 'bitstring' is a pointer to a contiguous memory block of type 'person'.

    Think of 'bitstring' like this:

    [person0][person1][person2] ... [person85]

    And each person is a struct or class, which you can access in the memory block with it's index number (to the right of 'person' in the example).

    So if you were to write:

    Code:
        int personInfo = bitstring->data[number];
    The computer won't know how to get to the 'data' member, because 'bitstring' isn't a struct or class, it's just a pointer to some memory you've allocated with keyword new. You need to reference the specific 'person' in 'bitstring' with an index:

    Code:
        // Assign 'pPerson' (a pointer to type person), with the address at the index 'number'
        // in the 'bitstring' memory block:
        person* pPerson = bitstring[number];
        
        // Now 'pPerson' is a pointer to the 'person' type you have stored at index 'number'.
        // Now you could access the 'person' type:
        if (true == pPerson->selected)
        {
            pPerson->data[10] += 1; // This change is reflected in: bitstring[number]->data[10]
        }
    
        // To access the data directly, without declaring the variable 'pPerson', you do:
        bitstring[index_of_person]->member_of_person;
    I hope this was clear. If not just let me know, pointers can be confusing for everyone!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alpo
    The variable 'bitstring' is a pointer to a contiguous memory block of type 'person'.
    No, in the code from post #1, bitstring is a pointer to a person object that happens to point to the first person object of a dynamic array of person objects.

    Quote Originally Posted by Alpo
    Code:
    // Assign 'pPerson' (a pointer to type person), with the address at the index 'number'
    // in the 'bitstring' memory block:
    person* pPerson = bitstring[number];
    Since bitstring is a pointer to a person object, it follows that bitstring[number], if it exists, is a person object. Therefore, the above code is wrong since it does not make sense to assign a person object to a pointer to a person object. This could be fixed with:
    Code:
    person* pPerson = bitstring + number;
    or this:
    Code:
    person* pPerson = &bitstring[number];
    But then I do not really see the point since one can just access bitstring[number]. If convenience is desired, then one might as well use a reference:
    Code:
    person& current_person = bitstring[number];
    That said, manual memory management can be easily avoided here, as in my example. If the number of elements must be variable, then one could change the std::array into a std::vector. If manual memory management must be used, then remember to delete[] what you new[]. Also, the name bitstring for what appears to be an array/list/container of person objects sounds weird, which is why my example renamed it to persons.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by laserlight View Post
    No, in the code from post #1, bitstring is a pointer to a person object that happens to point to the first person object of a dynamic array of person objects.
    Ah I see what you are saying. I was failing to take in the fact that 'bitstring' already holds the address of a person already.

    Quote Originally Posted by laserlight
    Code:
    person* pPerson = &bitstring[number];
    I was thinking of it as an array of pointers by that point. Thanks for pointing that out.

    Anyway, the reason I split the expression up (tried to at least), was that it's sometimes easier to see what an expression is evaluating to that way. For instance, if you know that (bitstring[10]) evaluates to a 'person' type, it could be easier to see what (bitstring[10].data[24]) evaluates to.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alpo
    Anyway, the reason I split the expression up (tried to at least), was that it's sometimes easier to see what an expression is evaluating to that way. For instance, if you know that (bitstring[10]) evaluates to a 'person' type, it could be easier to see what (bitstring[10].data[24]) evaluates to.
    Ah, but if the expression is complex enough for that to matter, maybe it would be better to define a helper function that will operate on a single person object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick sorting an array of nodes in a linked list.
    By HelpfulPerson in forum C Programming
    Replies: 3
    Last Post: 10-19-2013, 11:45 AM
  2. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  3. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  4. how do i create an array...
    By qubit67 in forum C Programming
    Replies: 9
    Last Post: 04-10-2007, 11:54 PM
  5. Array of nodes
    By swanley007 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2005, 04:11 PM

Tags for this Thread