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
- g++ ld: symbol(s) not found for architecture x86_64
- Making my own toString() method on c++ struct
- error: lvalue required as unary & operand
- What is use of c_str function In c++
- Is there a decent wait function in C++?
- Reverse Contents in Array
- Why would we call cin.clear() and cin.ignore() after reading input?
- Should I learn C before learning C++?
- Comparison with string literal results in unspecified behaviour?
- c++: No instance of overloaded function
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- getline() does not work if used after some inputs
- What is std::move(), and when should it be used?
- Undefined reference to class constructor, including .cpp file fixes
- Deleting a dynamically allocated 2D array
- How to add element to C++ array?
- Difference of keywords ‘typename’ and ‘class’ in templates?
- C++ strings and pointers
- What is the preferred way to include error messages in C++?
- How to Convert a C++ String to Uppercase
- How to convert an ASCII char to its ASCII int value?
- Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11?
- Reference to non-static member function must be called
- Call to non-static member function without an object argument compiler error
- C++ compile time error: expected identifier before numeric constant
- How can I assign an array from an initializer list?
- Converting string to ASCII
- got “cannot appear in a constant-expression” when using template
- C++ ostream and ofstream conversions
- C++ discards qualifiers
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- Friend methods error
- Does WPF Work with C++?
- Multiple inputs on one line
- Getting the error floating point exception: 8