Thread: vector of arrays of pointers to structures

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,412
    Incidentally, have you considered passing the vector<Blob> by reference instead of passing a pointer to vector<Blob>?
    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
    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,895
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 05: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, 03:07 AM