Array of 5 vectors?

Hey all, hope everyone is good..

For my program, i want to create an array of 5 vectors (and being a vector, i don't know how many elements each vector will have.

I just need to know the correct syntax. Instead of what i had originally:
1
2
3
4
5
vector<string> NOTE_DURATION_M1;
vector<string> NOTE_DURATION_M2;
vector<string> NOTE_DURATION_M3;
vector<string> NOTE_DURATION_M4;
vector<string> NOTE_DURATION_M5;


I tried:

vector<string> NOTE_DURATION[5];

...but this looks to me that i'm assigning only 5 elements to 1 vector. How do i access say the last element in the the 3rd vector.


In my code, the following kicks up operand errors:

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
// FOR: LOOP THROUGH ALL 5 MELODIES..
	for (int i = 0; i > 5; i++)
	{
		// FOR: LOOP TO CYCLE THROUGH EACH NOTE OF EACH MELODY..
		for (int j = 0; j < NOTE_DURATION[i].size(); j++)
		{
			// IF: IF ANY NOTE LENGTH VALUE IS 1 (BAR)
			if (NOTE_DURATION[j] == 1)
			{
				NOTE_DURATION[j] = BAR_TIMING_1;
			}
			// IF: IF ANY NOTE LENGTH VALUE IS 2 (HALF BAR)
			else if (NOTE_DURATION[j] == 2)
			{
				NOTE_DURATION[j] = BAR_TIMING_2;
			}
			// IF: IF ANY NOTE LENGTH VALUE IS 4 (1/4th)
			else if (NOTE_DURATION[j] == 4)
			{
				NOTE_DURATION[j] = BAR_TIMING_4;
			}
			// IF: IF ANY NOTE LENGTH VALUE IS 8 (1/8th)
			else if (NOTE_DURATION[j] == 8)
			{
				NOTE_DURATION[j] = BAR_TIMING_8;
			}
			// IF: IF ANY NOTE LENGTH VALUE IS 16 (1/16th)
			else if (NOTE_DURATION[j] == 16)
			{
				NOTE_DURATION[j] = BAR_TIMING_16;
			}
			// END IF..
		}
		// END FOR..
	}
	//END FOR.. 


Prior to this, the user will be entering either 1, 2, 4, 8 or 16 for the note length (unlimited amount of notes, hence a vector to hold them)..

Cheers,

Paul..
Last edited on
vector<string> NOTE_DURATION[5];
How do i access say the last element in the the 3rd vector.


NOTE_DURATION is an array of five vectors.

NOTE_DURATION[0] is the first vector.
NOTE_DURATION[1] is the second vector.
NOTE_DURATION[2] is the third vector.
NOTE_DURATION[3] is the fourth vector.
NOTE_DURATION[4] is the fifth vector.

So to get the 3rd vector, NOTE_DURATION[2]

To get the last element of a vector, use the vector class function back http://www.cplusplus.com/reference/vector/vector/back/

NOTE_DURATION[2].back() will give you the final element in this vector. Because it happens to be a vector of string objects, the final element is a string.

As an aside, why an array of vectors? Why not a vector of vectors?

vector< vector<string>> NOTE_DURATION;
Last edited on
Hey Moschops, thanks for the reply and the heads on the last element thing.

I only thought about an array of vectors as I know there's only going to be 5 melodies and therefore 5 vectors. I could do a vector of vectors but as i know its a fixed amount, i thought an array would be better.

So:

vector<string> NOTE_DURATION[5];

...is the right syntax then? ok cool

Why can i not use == or = if my if statements then? It says they are not the correct operand..

i is the loop for the 5 melodies and j is looping through the elements of the vectors, so is there any issues in indexing NOTE_DURATION[j] to access the elements in the vectors? Or am i confusing the program by indexing each array?


Paul..
If you try to use == and it doesn't work, it means that no == operator exists that takes the two objects you're trying to use it with. What are you trying to compare?

1
2
3
4
5
6
7
8
9
10
// FOR: LOOP THROUGH ALL 5 MELODIES..
    for (int i = 0; i > 5; i++)
    {
        // FOR: LOOP TO CYCLE THROUGH EACH NOTE OF EACH MELODY..
        for (int j = 0; j < NOTE_DURATION[i].size(); j++)
        {
            // IF: IF ANY NOTE LENGTH VALUE IS 1 (BAR)
            if (NOTE_DURATION[i][j] == 1)
            {
            // ... 


Although you probably want to compare to '1', and not 1.
Last edited on
That sorted it, cire :) thanks both of you
Topic archived. No new replies allowed.