Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/vectors/examples/vector_eg by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
An example of vectors.
Public permissions remove
Engine
Template / Reference solution
 
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main() {
   vector<string> words;     // CONSTRUCT
   words.push_back("Hello"); // ADD to back end
   words.push_back("World"); 
   cout << words.size() << endl; // get SIZE
   cout << words[0] << " " << words.at(1) << endl; // [i], at(): ACCESS
   
   vector<int> nums(10); // CONSTRUCT with initial size 10
   // or use: vector<int> nums; nums.resize(10);
   for (int i=0; i<10; i++)
      nums[i] = i*i; // assign
   cout << nums.back() << endl; // ACCESS from back end
   nums.pop_back(); // DELETE (pop) from back end
   
   // FYI: iterator-type iteration (stay tuned in CS 104) 
   // but... it is more straightforward to use a normal for loop with size()
   for (vector<int>::iterator it=nums.begin(); it != nums.end(); it++)
      cout << *it << " ";
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {}
]
Solution visibility remove
Is example?
i.e., just a demo
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.