Create vector, push_back element, then modify it as so:
struct subject { string name; int marks; int credits; }; int main() { vector<subject> sub; //Push back new subject created with default constructor. sub.push_back(subject()); //Vector now has 1 element @ index 0, so modify it. sub[0].name = "english"; //Add a new element if you want another: sub.push_back(subject()); //Modify its name and marks. sub[1].name = "math"; sub[1].marks = 90; }
You cant access a vector with [#] until an element exists in the vector at that index. This example populates the [#] and then modifies it afterward.
Related Posts:
- Displaying a vector of strings in C++
- How to implement 2D vector array?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Vector of Vectors to create matrix
- How to find out if an item is present in a std::vector?
- How do I print out the contents of a vector?
- Struct Constructor in C++?
- C++ struct constructor
- check if a std::vector contains a certain object?
- How to navigate through a vector using iterators? (C++)
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How do I reverse a C++ vector?
- Debug assertion failed. C++ vector subscript out of range
- No matching member function for call to ‘push_back’ error
- Pass a vector by reference C++
- Why there is no pop_front method in C++ std::vector?
- Initializing a two dimensional std::vector
- C++ – No matching member function for call to ‘push_back’
- Debug assertion failed. C++ vector subscript out of range
- Vector is not a Template?
- ‘vector’ in namespace ‘std’ does not have a template type
- C++ error: Undefined symbols for architecture x86_64
- Converting a vector
to string - error: expected primary-expression before ‘)’ token (C)
- vector
::size_type in C++ - Why use a new call with a C++ ‘vector’?
- Why doesn’t std::vector::push_front() exist?
- C++ delete vector, objects, free memory
- How to iterate over a vector?
- What are the differences between struct and class in C++?
- How to access the contents of a vector from a pointer to the vector in C++?
- Insert object at index of vector c++
- How to convert vector to array
- No operator << matches these operands
- Initialize empty vector in structure – c++
- splitting a string into an array in C++ without using vector
- What’s the most efficient way to erase duplicates and sort a vector?
- Best way to extract a subvector from a vector?
- Appending a vector to a vector
- C++ Expression must have pointer-to-object type
- How to initialize a vector of pointers
- Difference between ‘struct’ and ‘typedef struct’ in C++?
- C++ Erase vector element by value rather than by position?
- error: expected unqualified-id before ‘.’ token //(struct)
- Correct way to work with vector of arrays
- Function for C++ struct
- C++ error: double free or corruption (fasttop)
- error C2106: ‘=’ : left operand must be l-value
- I get this error: “glibc detected”
- How to return a struct from a function in C++?
- How to create a vector of class objects in C++?
- “vector” was not declared in this scope
- too many initializers for ‘int [0]’ c++
- Vector declaration “expected parameter declarator”
- Why is this vector iterator not incrementable?
- Struct inheritance in C++
- Why can’t I make a vector of references?
- Displaying contents of a vector container in C++
- What is the size of sizeof(vector)? C++
- c++ vector bubble sort
- clearing a vector of pointers [duplicate]
- Struct with template variables in C++
- “Cannot allocate an object of abstract type” error
- Is “delete this” allowed in C++?
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- How to create a dynamic array of integers
- How to create a dynamic array of integers
- Linker Error C++ “undefined reference ” [duplicate]
- How do I build a graphical user interface in C++? [closed]
- C++ convert from 1 char to string?
- How do I build a graphical user interface in C++? [closed]
- convert a char* to std::string
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- Why the switch statement cannot be applied on strings?
- What is the difference between g++ and gcc?
- How to use setprecision in C++
- How to dynamically allocate arrays in C++
- What does (~0L) mean?
- How to dynamically allocate arrays in C++
- What is the best way to use a HashMap in C++?
- What is the best way to use a HashMap in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- Sleep for milliseconds
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Easiest way to convert int to string in C++
- What is the difference between float and double?
- Why is “using namespace std;” considered bad practice?
- outputting ascii table in C++
- What is a lambda expression in C++11?
- How to throw a C++ exception
- std::string to char*
- How to generate a random number in C++?