题目笔记 UVA101(STL vector)

STL vector

UVA101      AC码

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

const int maxx=30;
int n;
string s1,s2;
vector <int> plie[maxx];

void find_bl(int a,int& p,int& h)            //这里用的是引用不太明白指针放这里一样不            
{
    for(p=0;p<n;p++)//错误在这里,找了一下午
    {
        for(h=0;h<plie[p].size();h++) 
        if(plie[p][h]==a) return;
    }
}

void clean_bl (int p,int h)
{
    for(int i=h+1;i<plie[p].size();i++) 
    {
        int x=plie[p][i];
        plie[x].push_back(x);
    }
    plie[p].resize(h+1);
}

void trans_bl(int p,int h,int t)
{
    for(int i=h;i<plie[p].size();i++) plie[t].push_back(plie[p][i]);
    plie[p].resize(h);
}

int main()
{
    int e,f;
    cin>>n;
    for(int i=0;i<n;i++) plie[i].push_back(i);//别忘了初始化哦
    while(cin>>s1>>e>>s2>>f)
    {
        int ep,eh,fp,fh;
        find_bl(e,ep,eh);
        find_bl(f,fp,fh);
        if(ep==fp) continue;
        if(s2=="onto") clean_bl(fp,fh);
        if(s1=="move") clean_bl(ep,eh);
        trans_bl(ep,eh,fp);
    }
    for(int i=0;i<n;i++) 
    {
        cout<<i<<":";
        for(int j=0;j<plie[i].size();j++) cout<<" "<<plie[i][j];
        cout<<endl;
    }
    return 0;
    
}

几个注意的地方

while输入

while(cin>>s1>>e>>s2>>f)
    {
       /* int ep,eh,fp,fh;
        find_bl(e,ep,eh);
        find_bl(f,fp,fh);
        if(ep==fp) continue;
        if(s2=="onto") clean_bl(fp,fh);
        if(s1=="move") clean_bl(ep,eh);
        trans_bl(ep,eh,fp);*/
        //操作
    }

c++引用

void find_bl(int a,int& p,int& h)            
{
    for(p=0;p<n;p++)//错误在这里,找了一下午
    {
        for(h=0;h<plie[p].size();h++) 
        if(plie[p][h]==a) return;
    }
}

vector常见用法

//假设a为一个vector
a.size()                        //读取大小
 
a.resize()                     //改变大小

a.push_back()              //向尾部添加元素

a.pop_back()               //删除尾部元素

a.clear()                      //清空

a.empty()                    //检测是否为空,为空则返回true
原文地址:https://www.cnblogs.com/juuich/p/12380277.html