Adding values to vector<int> , and printing values from a vector

I am attempting to find and print the odd integers, from a vector that holds integers.

I've looked at the reference on this site (http://www.cplusplus.com/reference/stl/vector/), and from that I learnt that I should be using the insert() method (http://www.cplusplus.com/reference/stl/vector/insert/)... but I can't get any further than that.

Originally I had tried things like numbers[0] = 1; and numbers.add(1);, before I found that page I linked to.

Code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string>
#include <iostream>
#include <vector>
using namespace std;

class PrintOdd
{
private:
	vector<int> numbers;
	int vectSize;
	
public:
	
	PrintOdd(vector<int> v)
	{
		numbers = v;
	}
	
	void main()
	{
		numbers.insert(0,4);
		numbers.insert(1,435);
		numbers.insert(2,2);
		numbers.insert(3,12);
		numbers.insert(4,56);
		
		numbers.insert(5,10);
		numbers.insert(6,7);
		numbers.insert(7,3);
		numbers.insert(8,1);
		numbers.insert(9,19);
	}
	
	void print()
	{
		vectSize = numbers.size();
		
		cout << "Odd numbers: ";
		
		for (int i=0; i<vectSize; i++)
		{
			if (numbers.at(i) % 2 > 0)
			{
				cout << numbers.at(i) << ", ";
			}
		}
		
		cout << endl;
		cout << "-- Checking Complete --" << endl;
	}
}


I'm having problems with adding the numbers to the vector, to begin with.

Dev-C++'s compiler produces this error message:
 
no matching function for call to `std::vector<int, std::allocator<int> >::insert(int, int)'  

So some help understanding what that means would be great ;-)

And if possible, could you have a quick look over my print() method as well to check that I've done that correctly?

Thanks

P.S. Any tips on how to add links to my post? I can't find a button at the bottom of the editor and phpBB forum tags don't seem to work here.
Last edited on
All three versions of the insert take an iterator as the first argument, not an int.

You need to do something like
numbers.insert(numbers.end(), 1); // insert 1 at the end
Okay, thanks. I'll have a go at using that code when I get back to the computer, and post back here with how I went.

Much appreciated.
I had another problem, resulting in this error:

expected unqualified-id at end of input

Found the answer to that one quickly though. Had to add a semi-colon to the last curly brace... which was a difference from Java I wasn't warned about ;-)

Thanks again for your help kempofighter. I'm marking this as solved.
Topic archived. No new replies allowed.