注册 登录
编程论坛 C++教室

[求助]请教c++关于vector容器的用法

keloy 发布于 2007-10-21 16:09, 948 次点击

在使用vector<>中我有一个小问题,比如vector<string>做string的数组,可以用pash_back()输入,我想知道,怎样把指针指到任何一个已经输入的string的头???

5 回复
#2
yuyunliuhen2007-10-21 16:18

reference front( );
const_reference front( ) const;
///////////////////////////////////
for example:
#include <vector>
#include <iostream>

int main( )
{
using namespace std;
vector <int> v1;

v1.push_back( 10 );
v1.push_back( 11 );
v1.push_back( 12 );

int& i = v1.front( );
const int& ii = v1.front( );

cout << "The first integer of v1 is "<< i << endl;
i++;
cout << "The second integer of v1 is "<< ii << endl;
}


output:
The first integer of v1 is 10
The second integer of v1 is 11

#3
keloy2007-10-21 16:33
谢谢了,还有个问题


const int& ii = v1.front( ); 这句是什么意思
#4
msshadow2007-10-21 20:08
是队头元素吧..
#5
msshadow2007-10-21 20:13

include <queue>
#include <iostream>
int main() {
using namespace std;
queue <int> q1;

q1.push( 10 );
q1.push( 20 );
q1.push( 30 );

queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl;

int& ii = q1.back( );
int& iii = q1.front( );

cout << "The integer at the back of queue q1 is " << ii
<< "." << endl;
cout << "The integer at the front of queue q1 is " << iii
<< "." << endl;
}

#6
评梅2007-10-25 21:58
学习
1