CSCI 1300 Chapter 9: Vectors

39 views3 pages

Document Summary

Use the size member function vectorname. size() to get the size of the vector for (int i = 0; i < values. size(); i++) cout << values[i] << endl; A vector of ten integers, filled with 1, 2, 3, , vector numbers; for (int i = 1; i <= 10; i++) numbers. push_back(i); vector numbers(10); for (int i = 0; i < numbers. size(); i++) Another way of defining a vector of ten integers and filling it with 1, 2, 3, , 10. numbers[i] = i + 1; Use the push_back member function to add more elements to a vector. When you remove an element from a vector, you want to adjust the size of the vector by calling the pop_back member function. Here is the code for removing an element at [pos] when the order doesn"t matter. int last_pos = values. size() - 1; values[pos] = values[last_pos]; // replace element at pos with last element values. pop_back(); // delete last element.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions