首页 > > 详细

C++辅导vector数据结构、辅导C++作、Programming 辅导留学生C/C++设计

Requirement
CS 162
Intro to Programming II
Vectors
1
Vectors
– A container from the Standard Template Library
– Holds a set of elements, like an array
– Flexible number of elements - can grow and
shrink
•  Similar to a dynamic array
• Must include vector header file to use vectors
#include
Vectors
• Can hold values of any type
– Type is specified when a vector is defined
vector scores;
vector volumes;
• Can use [] to access elements
• scores[3]
• volumes[0]
Defining Vectors
• Define a vector of integers (starts with 0
elements)
vector scores;
• Define int vector with initial size 30 elements
vector scores(30);
• Define 20-element int vector and initialize all
elements to 0
vector scores(20, 0);
• Define int vector initialized to size and contents
of vector finals
vector scores(finals);
Growing a Vector’s Size
• Use push_back member function to add
an element to a full array or to an array
that had no defined size
// Add a new element holding a 75
scores.push_back(75);
• Use size member function to determine
number of elements currently in a vector
howbig = scores.size();
Removing Vector Elements
• Use pop_back member function to
remove last element from vector
scores.pop_back();
• To remove all contents of vector, use
clear member function
scores.clear();
• To determine if vector is empty, use empty
member function
while (!scores.empty()) …
Vector Func

 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!