Defining a vector within a vector?

The compiler i use is Microsoft visual studio

Hello, I am attempting to become a programmer but I only have a few books of basic programming knowledge under my belt and I figured that it would be smart to code a small bit before I move on to other things.

In this code I am trying to make a digital version of mad libs. I decided to make a vector that holds vectors that hold strings. So in a sense I have a vector that holds a story and the story in turn holds sentences. Sadly when I tried to create a story (A vector within a vector) it told me that the definition for the vector (story) was "undefined". Now this may seem a bit complex (or it may not) but when I tried to reason my way though why my compiler was not recognizing the definition I found nothing.

As you can see in the code below I created a vector called "number" then I put the number 1 in the first slot. I didnt define a number 1 anywhere else in the code and the number 1 was put into slot 1 of "number" just fine. So I reasoned that it calls the declaration within the vector sort of like this.
number.push_back (int slot1 = 1)
After I reasoned this I thought that putting a vector in a vector should work the same way like this.
Stories.push_back (vector<string> Story1)
but since 1 didnt need the int slot1 = i guessed that I dont need vector<string> as well but it still gives me an error in the IDE.

This may seem like a simple question but I am completely stumped and any insight on what I need to do or what I am doing wrong would be greatly appreciated. Thank you :)



1
2
3
4
5
6
7
8
9
10
  int main()
{
	vector<int> number;
	number.push_back(1);

	vector<vector<string>> Stories;

	// ------------------------------------------- Story 1
	Stories.push_back(Story1);
		Story1.push_back("Once upon a time there was a ");
thats because you pushed a "literal" into the vector, when you pusha variable, it has to exist.

so you need to declare Story1 first, like this...

1
2
vector<string> Story1 = {"Once", "upon", "a", "time"};
Stories.push_back(Story1);

Jaybob66 wrote:
thats because you pushed a "literal" into the vector, when you pusha variable, it has to exist.


No.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::vector<std::string>> stories;
    stories.push_back({"Once", "upon", "a", "time"});

    for (const auto& x : stories)
        for (const std::string& str : x)
            std::cout << str << ' ';

    return 0;
}


http://ideone.com/KK6geY

Basically, the push_back function of a vector can be used to create an object of its type inside itself. It doesn't have to be an lvalue, it can bind to an rvalue too.

@OP
I have no idea what you are doing in your code. Just because you assume it should have a name doesn't mean that you can use that name in your code: '1' is a predefined constant. Also, because push_back creates a copy of the object, pushing back the sentence and then filling the sentence makes no sense anyway.
Last edited on
why "NO" ?

you just did exactly what i said, pushed back literals.
A string can hold spaces, so why do you need a vector of strings to hold individual words. Try to print that out and they would run together...
Topic archived. No new replies allowed.