how to use C++ Vectors ?

用java做一个2048小游戏 昨天,自己突发奇想,能不能在忙碌的学习里抽一点时间来做一点事情,然后,我就有了一个要做一个2048小游戏的想法。说真的,java我已经认真学习了一段时间了,现在也刚刚学到Swing,虽然做这个2048还有很多东西没有学习过,但是,一边学一边做才是我们菜鸟的样子吧。 做这个东西,自己没有什么经验,我是这样想的:先做个需求出来吧,然后就有了第一部分的需求了;再按照需求,把开始部分想法做出来。 pa 阅读详情

Syntax:
  #include <vector>
  container();
  container( const container& c );
  container( size_type num, const TYPE& val = TYPE() );
  container( input_iterator start, input_iterator end );
  ~container();

The default vector constructor takes no arguments, creates a new instance of that vector.

The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector c.

The third constructor creates a vector with space for num objects. If val is specified, each of those objects will be given that value. For example, the following code creates a vector consisting of five copies of the integer 42:

 vector<int> v1( 5, 42 );		

The last constructor creates a vector that is initialized to contain the elements between start and end. For example:

 // create a vector of random integers
 cout << "original vector: ";
 vector<int> v;
 for( int i = 0; i < 10; i++ ) {
   int num = (int) rand() % 10;
   cout << num << " ";
   v.push_back( num );
 }
 cout << endl;
 // find the first element of v that is even
 vector<int>::iterator iter1 = v.begin();
 while( iter1 != v.end() && *iter1 % 2 != 0 ) {
   iter1++;
 }
 // find the last element of v that is even
 vector<int>::iterator iter2 = v.end();
 do {
   iter2--;
 } while( iter2 != v.begin() && *iter2 % 2 != 0 );
 cout << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl;
 cout << "new vector: ";
 vector<int> v2( iter1, iter2 );
 for( int i = 0; i < v2.size(); i++ ) {
   cout << v2[i] << " ";
 }
 cout << endl;		

When run, this code displays the following output:

 original vector: 1 9 7 9 2 7 2 1 9 8
 first even number: 2, last even number: 8
 new vector: 2 7 2 1 9		

All of these constructors run in linear time except the first, which runs in constant time.

The default destructor is called when the vector should be destroyed.


Syntax:
  #include <vector>
  TYPE& operator[]( size_type index );
  const TYPE& operator[]( size_type index ) const;
  container operator=(const container& c2);
  bool operator==(const container& c1, const container& c2);
  bool operator!=(const container& c1, const container& c2);
  bool operator<(const container& c1, const container& c2);
  bool operator>(const container& c1, const container& c2);
  bool operator<=(const container& c1, const container& c2);
  bool operator>=(const container& c1, const container& c2);

All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a vector can be examined with the [] operator.

Performing a comparison or assigning one vector to another takes linear time. The [] operator runs in constant time.

Two `containers` are equal if:

  1. Their size is the same, and
  2. Each member in location i in one vector is equal to the the member in location i in the other vector.

Comparisons among vectors are done lexicographically.

For example, the following code uses the [] operator to access all of the elements of a vector:

 vector<int> v( 5, 1 );
 for( int i = 0; i < v.size(); i++ ) {
   cout << "Element " << i << " is " << v[i] << endl;
 }		


Syntax:
  #include <vector>
  void assign( size_type num, const TYPE& val );
  void assign( input_iterator start, input_iterator end );

The assign() function either gives the current vector the values from start to end, or gives it num copies of val.

This function will destroy the previous contents of the vector.

For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:

 vector<int> v;
 v.assign( 10, 42 );
 for( int i = 0; i < v.size(); i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;		

The above code displays the following output:

 42 42 42 42 42 42 42 42 42 42		

The next example shows how assign() can be used to copy one vector to another:

 vector<int> v1;
 for( int i = 0; i < 10; i++ ) {
   v1.push_back( i );
 }
 vector<int> v2;
 v2.assign( v1.begin(), v1.end() );
 for( int i = 0; i < v2.size(); i++ ) {
   cout << v2[i] << " ";
 }
 cout << endl;		

When run, the above code displays the following output:

 0 1 2 3 4 5 6 7 8 9		


at
Syntax:
  #include <vector>
  TYPE& at( size_type loc );
  const TYPE& at( size_type loc ) const;

The at() function returns a reference to the element in the vector at index loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.

For example, consider the following code:

 vector<int> v( 5, 1 );
 for( int i = 0; i < 10; i++ ) {
   cout << "Element " << i << " is " << v[i] << endl;
 }		

This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:

 vector<int> v( 5, 1 );
 for( int i = 0; i < 10; i++ ) {
   cout << "Element " << i << " is " << v.at(i) << endl;
 }		

Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.


Syntax:
  #include <vector>
  TYPE& back();
  const TYPE& back() const;

The back() function returns a reference to the last element in the vector.

For example:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 cout << "The first element is " << v.front()
      << " and the last element is " << v.back() << endl;		

This code produces the following output:

 The first element is 0 and the last element is 4		

The back() function runs in constant time.


Syntax:
  #include <vector>
  iterator begin();
  const_iterator begin() const;

The function begin() returns an iterator to the first element of the vector. begin() should run in constant time.

For example, the following code uses begin() to initialize an iterator that is used to traverse a list:

   // Create a list of characters
   list<char> charList;
   for( int i=0; i < 10; i++ ) {
     charList.push_front( i + 65 );
   }
   // Display the list
   list<char>::iterator theIterator;
   for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) {
     cout << *theIterator;
   }		


Syntax:
  #include <vector>
  size_type capacity() const;

The capacity() function returns the number of elements that the vector can hold before it will need to allocate more space.

For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:

 vector<int> v1(10);
 cout << "The capacity of v1 is " << v1.capacity() << endl;
 vector<int> v2;
 v2.reserve(20);
 cout << "The capacity of v2 is " << v2.capacity() << endl;		

When run, the above code produces the following output:

 The capacity of v1 is 10
 The capacity of v2 is 20		

C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the reserve() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.

The capacity() function runs in constant time.


Syntax:
  #include <vector>
  void clear();

The function clear() deletes all of the elements in the vector. clear() runs in linear time.


Syntax:
  #include <vector>
  bool empty() const;

The empty() function returns true if the vector has no elements, false otherwise.

For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a vector and display its contents in reverse order:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout << v.back() << endl;
   v.pop_back();
 }		


end
Syntax:
  #include <vector>
  iterator end();
  const_iterator end() const;

The end() function returns an iterator just past the end of the vector.

Note that before you can access the last element of the vector using an iterator that you get from a call to end(), you'll have to decrement the iterator first.

For example, the following code uses begin() and end() to iterate through all of the members of a vector:

 vector<int> v1( 5, 789 );
 vector<int>::iterator it;
 for( it = v1.begin(); it != v1.end(); it++ ) {
   cout << *it << endl;
 }		

The iterator is initialized with a call to begin(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the vector, the loop will only stop once all of the elements of the vector have been displayed.

end() runs in constant time.


Syntax:
  #include <vector>
  iterator erase( iterator loc );
  iterator erase( iterator start, iterator end );

The erase() function either deletes the element at location loc, or deletes the elements between start and end. The return value is the element after the last element erased.

The first version of erase (the version that deletes a single element at location loc) runs in constant time for lists and linear time for vectors, dequeues, and strings. The multiple-element version of erase always takes linear time.

For example:

 // Create a vector, load it with the first ten characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 int size = alphaVector.size();
 vector<char>::iterator startIterator;
 vector<char>::iterator tempIterator;
 for( int i=0; i < size; i++ ) {
   startIterator = alphaVector.begin();
   alphaVector.erase( startIterator );
   // Display the vector
   for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) {
     cout << *tempIterator;
   }
   cout << endl;
 }		

That code would display the following output:

 BCDEFGHIJ
 CDEFGHIJ
 DEFGHIJ
 EFGHIJ
 FGHIJ
 GHIJ
 HIJ
 IJ
 J		

In the next example, erase() is called with two iterators to delete a range of elements from a vector:

 // create a vector, load it with the first ten characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 // display the complete vector
 for( int i = 0; i < alphaVector.size(); i++ ) {
   cout << alphaVector[i];
 }
 cout << endl;
 // use erase to remove all but the first two and last three elements
 // of the vector
 alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 );
 // display the modified vector
 for( int i = 0; i < alphaVector.size(); i++ ) {
   cout << alphaVector[i];
 }
 cout << endl;		

When run, the above code displays:

 ABCDEFGHIJ
 ABHIJ		


Syntax:
  #include <vector>
  TYPE& front();
  const TYPE& front() const;

The front() function returns a reference to the first element of the vector, and runs in constant time.


Syntax:
  #include <vector>
  iterator insert( iterator loc, const TYPE& val );
  void insert( iterator loc, size_type num, const TYPE& val );
  template<TYPE> void insert( iterator loc, input_iterator start, input_iterator end );

The insert() function either:

  • inserts val before loc, returning an iterator to the element inserted,
  • inserts num copies of val before loc, or
  • inserts the elements from start to end before loc.

For example:

 // Create a vector, load it with the first 10 characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 // Insert four C's into the vector
 vector<char>::iterator theIterator = alphaVector.begin();
 alphaVector.insert( theIterator, 4, 'C' );
 // Display the vector
 for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )    {
   cout << *theIterator;
 }		

This code would display:

 CCCCABCDEFGHIJ		


Syntax:
  #include <vector>
  size_type max_size() const;

The max_size() function returns the maximum number of elements that the vector can hold.


Syntax:
  #include <vector>
  void pop_back();

The pop_back() function removes the last element of the vector.

pop_back() runs in constant time.


Syntax:
  #include <vector>
  void push_back( const TYPE& val );

The push_back() function appends val to the end of the vector.

For example, the following code puts 10 integers into a list:

   list<int> the_list;
   for( int i = 0; i < 10; i++ )
     the_list.push_back( i );		

When displayed, the resulting list would look like this:

 0 1 2 3 4 5 6 7 8 9		

push_back() runs in constant time.


Syntax:
  #include <vector>
  reverse_iterator rbegin();
  const_reverse_iterator rbegin() const;

The rbegin() function returns a reverse_iterator to the end of the current vector.

rbegin() runs in constant time.


Syntax:
  #include <vector>
  reverse_iterator rend();
  const_reverse_iterator rend() const;

The function rend() returns a reverse_iterator to the beginning of the current vector.

rend() runs in constant time.


Syntax:
  #include <vector>
  void reserve( size_type size );

The reserve() function sets the capacity of the vector to at least size.

reserve() runs in linear time.


Syntax:
  #include <vector>
  void resize( size_type num, const TYPE& val = TYPE() );

The function resize() changes the size of the vector to size. If val is specified then any newly-created elements will be initialized to have a value of val.

This function runs in linear time.


Syntax:
  #include <vector>
  size_type size() const;

The size() function returns the number of elements in the current vector.


Syntax:
  #include <vector>
  void swap( const container& from );

The swap() function exchanges the elements of the current vector with those of from. This function operates in constant time.

For example, the following code uses the swap() function to exchange the values of two strings:

   string first( "This comes first" );
   string second( "And this is second" );
   first.swap( second );
   cout << first << endl;
   cout << second << endl;		

The above code displays:

   And this is second
   This comes first		

利用Python做一个简单的对战小游戏 利用Python做一个简单的文字对战小游戏 一、游戏介绍 1、大体介绍:文字版的对战小游戏,可以利用Python随机生成两个角色,角色带有各自的血量和攻击值两个指标。两人在对战时同时攻击对方,同时造成与攻击值相等的伤害。最终一方血量降到0以下的为输。 2、拆解项目 ①ver1.0... 阅读详情

相关推荐

我在公司做的一个小游戏的总结感想

      这次这个小游戏真的让我感受到了学校与公司的最大不同。在学校里的时候我们给学校交学费,所以老师的任务就是教会我们东西,学习的时候不懂就问他,实在太笨了就坐下来给你写代码。而在公司是给我们钱,分下来的任务,不管你会不会能不能做出来,在规定的时间你反正就得交一份满意的答案给他,不行的话,你就滚蛋。       以前的经历让我自以为抗压能力很强,所以每次面试的时候我都很自豪的对面试官说

pby_bob的专栏 6238

做一个抽奖小游戏

首先,要有随机的数 ,所以会用到srand随机数种子,要用到#include<stdlib.h>和#include<time.h>,但是,节省一点,推荐用#include<bits/stdc++.h>万能头来使用。每周,我都会写一篇文章,今天,就做一个抽奖小游戏吧!

wyz_666666的博客 384

html做的小游戏,用Html做一个“快乐鸟”小游戏

入门学习前端的时候做了这么一个简单的小游戏,为了将自己所学到的知识运用起来。这里可以看出还是有BUG的,死亡动画不是在死亡的瞬间触发做这个小游戏用到了:htmlcssjQuery然后这个小游戏的图片素材用到了:其实用原生的js也是可以完成的,但是为什么要用jQuery?因为我直接跳过了原生JS的学习...只有等空余的时候,好好的找本书研究一下原生的js。下面是这个小游戏的大致流程:其中鸟和管道应该...

weixin_30420175的博客 2430

用python做一个小游戏代码,用python做简单的小游戏

大家好,小编为大家解答用python做一个小游戏的代码怎么搞的问题。很多人还不知道用python做一个小游戏,在1和10,现在让我们一起来看看吧!大家好,我是辣条。今天给大家带来30个py小游戏,一定要收藏!目录有手就行1、吃金币2、打乒乓3、滑雪4、并夕夕版飞机大战5、打地鼠简简单单6、小恐龙7、消消乐8、俄罗斯方块9、贪吃蛇普普通通10、24点小游戏11、平衡木12、外星人入侵13、贪心鸟14、井字棋888‘’有点困难15、炸弹人16、保卫森林17、五子棋18、吃豆豆19、坦克大战20、超级玛丽21、水

2301_81837718的博客 2962

用python做一个小游戏代码,python做小游戏代码300行

这篇文章主要介绍了python代码可以编写的小游戏有哪些,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。今天给大家带来30个py小游戏,一定要收藏! 全部源码都整理好了,在文章最底部~【有手就行系列不介绍玩法了+附源码】 源码分享: 2、打乒乓 【简单介绍一下玩法+附源码】玩法:上下控制起跳躲避 玩法:三个相连就能消除 玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速的。 玩法:童年经典,普通魔术也没啥意思,小时候玩的也是加速的pyt

2301_81900439的博客 1683

用Python做一个滑雪小游戏

我们成功地创建了一个简单的滑雪小游戏,并对Python的游戏开发有了初步的了解。在这个游戏中,我们利用pygame库提供的功能,绘制游戏窗口、加载图像、响应玩家输入、做碰撞检测等。游戏是让人娱乐和放松的好方式,而编写和玩自己的游戏则是一种特别有趣的体验。通过这个小游戏项目,我们将学习如何使用Python编程语言来制作自己的游戏,并且享受创造和玩耍的乐趣。游戏会根据玩家的得分来增加障碍物的速度和难度。我们还可以进一步扩展和优化它,添加更多的功能、关卡、特效,提高游戏的难度和趣味性。

这家伙很懒,什么都没有留下 1901

android做一个坦克大战小游戏_如何用MATLAB做一个2048小游戏

最近小编想尝试用一下matlab的gui界面,所以用matlab制作了一个2048小游戏,学习怎么做gui。2048的玩法想必大家还记得,每次可以选择上下左右滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢,系统也会在空白的地方随机出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。不断的叠加最终拼凑出2048这个数字就算成功。怎么用matlab来制作2048小游戏呢?第一步,先规...

weixin_39892309的博客 597

OriginPro实战:5步搞定碳材料拉曼光谱D峰G峰分析(附完整数据处理流程)

本文详细介绍了使用OriginPro进行碳材料拉曼光谱D峰G峰分析的完整流程,涵盖数据预处理、多图层可视化、高精度分峰拟合等关键步骤。特别针对材料科学研究中的基线漂移、多峰重叠等问题提供专业解决方案,帮助研究者快速完成从原始数据到科研级图表的全流程分析,提升碳材料表征效率与准确性。

vodka的博客 942

使用PixiJS做一个小游戏

PixiJS PixiJS使用WebGL,是一个超快的HTML5 2D渲染引擎。作为一个Javascript的2D渲染器,Pixi.js的目标是提供一个快速的、轻量级而且是兼任所有设备的2D库。 官方网址: http://www.pixijs.com/ 知识点 做一个小游戏,我们使用到PixiJS的功能不多,只需要了解以下几个点即可快速上手。 PIXI.Application 创建一个游...

JiaMaoforeach的专栏 1968

ESXI 8.0虚拟机安装OpenWRT软路由详细教程

OpenWrt 是嵌入式的 Linux 路由器固件,OpenWrt在稳定运行的同时提供了强大的扩展能力,用户可以完全的定制属于自己的路由系统,满足不一样的个性化需求,本文讲解如何使用ESXI安装OpenWRT,打造属于自己的软路由。本次安装使用的是版本。默认登录信息:用户:root密码:password下载固件:https://github.com/haiibo/OpenWrt/releases/tag/X86_64【CPU配置】:4核心。

qq_27121281的博客 3915

Synology File Station 官方 API 指南总结(中文版)

本文总结了Synology File Station官方API指南的核心内容,详细介绍了如何通过HTTP/HTTPS请求与DSM系统交互实现文件管理功能。主要内容包括: 基础API操作流程:从获取API信息、登录认证到执行请求和登出的完整工作流 请求构造与响应解析:JSON格式的请求参数处理和响应结果分析 关键API功能分类: 文件信息获取(List/Info) 文件搜索(Search) 收藏夹管理(Favorite) 缩略图生成(Thumb) 目录大小计算(DirSize) 文件上传(Upload)等 错

weixin_46001686的博客 2460

用python做一个小游戏代码,如何用python做一个游戏

不会python就不能用python开发入门级的小游戏?当然不是,我收集了十个python入门小游戏的源码和教程,并且即使你没有python基础,只要跟着这十个小游戏的开发详细教程去做,以及有了全部的源码,那就能自己开发出这样十个python入门小游戏python流星雨代码。不仅如此,在玩好这十个小游戏的过程中,还可以掌握python的基础知识点哦!完整的源码已经打包好了,需要练手的新手朋友,可以点击下面的链接领取!下面看看这十个小游戏具体是哪些吧~(底部有完整源码和教程)

jaskonyee的博客 630

(超详细)互补格雷码+相移码求解包裹相位(Python实现)

记录实现“互补格雷码+相移码”求解包裹相位的编码过程。也可以叫它“5+4”法,5代表五幅格雷码图像,4代表四步相移法的四张正弦条纹图。

暴雨梨花博客 3万+

用python做简单的小游戏,用python写一个小游戏

大家好,小编来为大家解答以下问题,初学者怎么用python写简单小游戏视频,初学者怎么用python写简单小游戏教程,现在让我们一起来看看吧!。

小发猫 536

Mybatis面试题(基础篇)

(1)mybatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架。(2)mybatis避免了几乎所有的JDBC代码(说明了mybatis底层用的就是JDBC,只不过是封装了)和手动设置参数以及获取结果集。(3)mybatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的pojo映射成数据库中的记录。

qq_38196449的博客 1687

python如何给声音增加回声效果

创建任何声音对象的回声效果。使用sndarray和numpy来创建原始声音的偏移和衰减副本。目前它只是使用硬编码的值来设置回声的数量和延迟。你可以根据自己的需要轻松地重新创建。这段注释说明了make_echo函数的用途和实现方式,即通过pygame的sndarray模块和numpy库来处理声音数据,生成具有回声效果的声音。函数中使用了固定的回声数量和延迟时间,但这些值可以根据用户的需求进行调整。

zhangzhechun的专栏 977

用python做一个小游戏代码,用python制作一个小游戏

他需要拥有可以移动的能力,这里设置了一个模拟移动的选择,是为了通过模拟移动判断它是否可以上、下、左、右移动。今天呢,给大家展示一下Python有趣的小地方,展示给大家看看,我做的小游戏,是否有大家小时候的回忆呢。大家好,小编来为大家解答以下问题,如何用python编写一个简单的小游戏,用python做一个小游戏代码,今天让我们一起来看看吧!或者私信我都可以,评论必回,私信必看,不错过任何一个认真学习的同学!由于游戏涉及到碰撞测试,所以我们先来定义一些游戏精灵类,包括推箱子的人、箱子、墙和目标位置指示标记。

小发猫 1968

【100%通过率】华为od机试C卷【小朋友来自多少小区】JS 实现

幼儿园组织活动,老师布置了一个任务:每个小朋友去了解与自己同一个小区的小朋友还有几个.我们将这些数量汇总到数组 garden 中。请根据这些小朋友给出的信息,计算班级小朋友至少来自几个小区?

MISAYAONE的博客 2007
上一篇: 在OpenGL中实现Extrude 造型
下一篇: get osnap points using ObjectARX
cgdtech
博客等级 码龄21年 1粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值