Joe Elder
Joe Elder

Reputation: 95

Creating a standard queue of pointers in c++

Say I have a queue of integers,

#include <iostream>
#include <queue>
using namespace std;


int main() {

    int firstValToBePushed = 1;

    queue<int> CheckoutLine;

    CheckoutLine.push(firstValeToBePushed);

    cout << CheckoutLine.front();

    return 0;
}

How can I do essentially the same thing using a queue which holds pointers to integers as opposed to integers like it currently does above. I plan on making a loop to make more than one value but this is just a more simple example.

Thanks,

Upvotes: 5

Views: 30369

Answers (4)

mcvz
mcvz

Reputation: 494

Adding a loop for you.

#include <iostream>
#include <queue>
using namespace std;

int main() {

queue<int*> theQueue;
char c = 'n';

while (c == 'n') {
  cout << "Enter \'n\' to add a new number to queue ( \'q\' to quit):";
  cin >> c;
  if ( c == 'q') {
    break;
  }
  else {
    int num;
    cout << "Enter an integer and press return: ";
    cin >> num;
    theQueue.push(new int(num));
  }
}

while( !theQueue.empty() ) {
  cout << theQueue.front() << ": " << *theQueue.front() << endl;
      delete theQueue.front();
  theQueue.pop();
}
return 0;
}

Upvotes: 3

user90843
user90843

Reputation:

If that's for lifetime management, then:

std::queue<std::shared_ptr<int>> CheckoutLine;
CheckoutLine.push(std::make_shared<int>(firstValeToBePushed))

If your queue is kind like a proxy, and someone else actually owns the lifetime of the objects, then definitely:

std::queue<std::reference_wrapper<int>> CheckoutLine;
CheckoutLine.push(firstValeToBePushed)

If you do not expose the queue anywhere and it's internal, then storing pointers is fine, as others suggested.

However, NEVER EVER expose to a client a collection of pointers, that's the worst thing one can do as you leave the burden of managing the lifetime on them, and that's the messier on collections.

Of course for primitive types or PODs, just copying is fine, no need to store pointers. Move semantics makes it easy even for non-PODs, unless you have some tricky construction or you object cannot implement move semantics.

#include <functional> for std::reference_wrapper and #include <memory> for std::shared_ptr, std::unique_ptr and friends. I'll assume you have access to a modern compiler.

Upvotes: 5

phschoen
phschoen

Reputation: 2081

#include <iostream>
#include <queue>
using namespace std;


int main()
{
int  value = 1337;

int* firstValeToBePushed = &value;


queue<int*> CheckoutLine;



CheckoutLine.push(firstValeToBePushed);


cout << *(CheckoutLine.front()) << "is at " << CheckoutLine.front();

return 0;

}

Upvotes: 1

I'm not sure to understand, maybe you want do that:

#include <iostream>
#include <queue>

using namespace std;

int main(){
    int firstValueToBePushed = 1;

    queue<int *> CheckoutLine;

    CheckoutLine.push(new int(firstValueToBePushed));

    cout << *CheckoutLine.front() << endl;

    return 0;
}

Upvotes: 0

Related Questions