vector of arrays of pointers to structures

This is a discussion on vector of arrays of pointers to structures within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a function as below: Code: void f ( std::vector<Blob> *blobsPtr ) { // create the vector to ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    vector of arrays of pointers to structures

    Hi,

    I have a function as below:

    Code:
    void f ( std::vector<Blob> *blobsPtr )
    {
    	// create the vector to store every pair of pointers to Blobs
    	vector<Blob*[2]> pairs;
    
    	// create a vector of pairs of Blob pointers, total of (num of Blobs choose 2)
    	for (vector<Blob>::size_type i = 0; i < blobsPtr->size() - 1; i++)
    		for (vector<Blob>::size_type j = 0; j < blobsPtr->size(); j++)
    		{
    			const Blob *b[2];
    			b[0] = &(blobsPtr->at(i));
    			b[1] = &(blobsPtr->at(j));
    			pairs.push_back(b);
    		}
    
    	// examine each pair and remove ones that fail the test
    }
    Where Blob is a simple structure.
    The goal of the function is to take all of the elements in the vector referenced by blobsPtr, and first create a vector of pairs of pointers to Blobs. In other words, create a vector that has every pairwise combination of Blobs (as pointers). Then I want to do some tests and remove the elements of 'pairs' that fail the tests. Finally, I want to delete all the Blobs that failed the test from the vector pointed to by 'blobsPtr'.

    Whew!

    The error I am getting with the current version is "cannot convert parameter 1 from 'const Blob *[2]' to 'Blob *const (&)[2]" when I try to do the push_back.
    I'm starting to get confused, so any help would be appreciated. Or perhaps I'm going about this totally wrong. The thing is I don't want to copy all the Blobs when I'm creating the pairwise vector, because it's just a waste of memory.

    Thanks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    If you want a pair of things, then use a std:: pair, that's what it's for. Not those array[2] things.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Cool, thanks. I'll look up std:: pair and hack around for a bit. That should simplify things.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Well, std:: pair seemed to help out. Here's what I came up with, which seems to work.

    Code:
    void filterPairwise(const IplImage *im, vector<Blob> *blobsPtr)
    {
    	// create the vector to store every pair of pointers to Blobs
    	vector< pair<Blob*, Blob*> > pairs;
    
    	// fill in the vector with every pairwise combination ( total of blobsPtr->size() choose 2 )
    	for (vector<Blob>::size_type i = 0; i < blobsPtr->size() - 1; i++)
    		for (vector<Blob>::size_type j = 0; j < blobsPtr->size(); j++)
    			pairs.push_back( pair<Blob*, Blob*>(&blobsPtr->at(i), &blobsPtr->at(j)) );
    
    	// examine each pair and remove ones that fail the test
    }

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Glad it helped.

    Code:
    pairs.push_back( pair<Blob*, Blob*>(&blobsPtr->at(i), &blobsPtr->at(j)) );
    You can simplify this line of code by making use of the std::make_pair convenience function:

    Code:
    pairs.push_back( make_pair(&blobsPtr->at(i), &blobsPtr->at(j)) );
    That avoids having to type Blob* over and over again.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Incidentally, have you considered passing the vector<Blob> by reference instead of passing a pointer to vector<Blob>?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    How would I use a reference? Do you mean in the parameters of the function?

    Would it be:
    Code:
    void f ( std::vector<Blob> &blobsPtr )
    ?

    And would calling the function be different? Right now I have:
    Code:
    vector<Blob> B;
    f(&B);
    Using a reference, I could still directly modify the vector in my function right? It's just simplifying the syntax a bit?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    vector<Blob> B;
    f(B);
    >Using a reference, I could still directly modify the vector in my function right? It's just simplifying the syntax a bit?

    yep, pretty much.

    http://www.cprogramming.com/tutorial/references.html

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Hmm... I've just realized that I need to add a third parameter to each vector element - a double: the "score" of each pair. Then I want to sort the vector based on the score.
    Is there such a thing as a std:: triplet? Or should I use a pair of pairs?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's std::tr1::tuple, but not many compilers support it yet. You could use Boost.Tuple.

    But actually, maybe you should just make a small struct.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Could someone remind me why this doesn't work? I don't know much about constructors and such.

    Code:
    typedef struct 
    {
    	int a;
            // etc... all primitive types
    }  Blob;
    
    
    typedef struct 
    {
    	Blob &blobA;
    	Blob &blobB;
    	double d;
    } BlobPair;
    I get
    Code:
    'BlobPair' : no appropriate default constructor available
    binary '=' : no operator found which takes a right-hand operand of type 'Blob *__w64 ' (or there is no acceptable conversion)
    etc...
    I just want the bare minimum for compiling. I don't know how to make all the constructors, deconstructors, operators etc... I will have to learn that at a later date.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think it's because references need to have a value. Not like pointers, which can have an undefined value. So you need a constructor to assign them, but it isn't there.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Great, thanks. I'll just switch them to pointers and keep it simple for now.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Indeed, robwhite is right. References are most easily used as function arguments where a argument is required. Otherwise you need to use a constructor that initializes the references.
    And you also seem to be assigning like = &var, which is right. It should = var.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 06:57 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. using pointers to pass multi-d arrays
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 04-27-2004, 04:07 AM

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