C/C++教程

C++primer 练习随便写写

本文主要是介绍C++primer 练习随便写写,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include<iostream>
using namespace std;
int main()
{
    int value,sum=0;
    while(cin>>value)
    {
        sum+=value;
    }
    cout<<sum<<endl;
    system("pause");
    return 0;
}
1.16

 

#include<iostream>
using namespace std;
int main()
{
    int a=0,b=0,c=0,i=0,j=0;
    cout<<"请输入俩数:"<<endl;
    cin>>a;cin>>b;
    if(a<b)
    {
        for(i=1;i<b-a;i++)
        cout<<a+i<<"    ";
    }
    else if(a>b)
    for(i=1;i<a-b;i++)
    cout<<b+i<<"    ";
    else cout<<"没有"<<endl;
    system("pause");
    return 0;
}
1.19

 

//输入vactor单词输出其大写
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<string> v1;
    string a,b;
    cout<<"pleadse input some words \n";
    while (cin>>a)
    {
        v1.push_back(a);
        cout<<"again?(y/n) \n";
        cin>>b;
        if(b=="n"||b=="N")
        break;
    }
    cout<<"your words:"<<endl;
    for(auto i:v1)
    cout<<a<<endl;

    cout<<"\n\nthe upper:";
    for(auto &j:v1)         //j 是v1值的引用,此处为strin型
    {
        for(auto &k:j)      //k 是j的值的引用,此处为char型
        k=toupper(k);       //变大写
        cout<<j<<endl;
    }

    system("pause");
    return 0;
}
3.17输入vactor单词输出其大写

 

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int temp,sum;
    vector<int> v1;
    cout<<"please input 10 nums,separate with space!\n";
    for(int i=0;i<10&&cin>>temp;i++)
    v1.push_back(temp);
    cout<<"your nums is:\n";
    for(auto a:v1)
    cout<<a<<endl;;

    cout<<"the sum type1:\n";
    for(int i=0;i<10;i+=2)
    cout<<v1[i]+v1[i+1]<<endl;;

    cout<<"the sum type2:\n";
    for(int i=0;i<5;i++)
    cout<<v1[i]+v1[9-i]<<endl;
    
    system("pause");
    return 0;
}
3.20输入vector数,输出2种形式的和

 

//输出下列v的容量和内容,使用迭代器,并判断是否与自己想的一样。 
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int num;
    vector<int> v1;//int型v1啥也没有
    vector<int> v2(10); //v2有10个元素,均为0
    vector<int> v3(10,42);//v3有10个元素,值均为42
    vector<int> v4{10};//v4有一个元素,值为10
    vector<int> v5{10,42};//v5有俩元素,值10,42
    vector<string> v6{10};//10个空串
    vector<string> v7{10,"hi"};//10个串,值均为hi

    cout<<"v1's size is "<<v1.size()<<endl;
    cout<<"v1's members: ";
    for(auto it=v1.cbegin();it!=v1.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv2's size is "<<v2.size()<<endl;
    cout<<"v2's members: ";
    for(auto it=v2.cbegin();it!=v2.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv3's size is "<<v3.size()<<endl;
    cout<<"v3's members: ";
    for(auto it=v3.cbegin();it!=v3.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv4's size is "<<v4.size()<<endl;
    cout<<"v4's members: ";
    for(auto it=v4.cbegin();it!=v4.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv5's size is "<<v5.size()<<endl;
    cout<<"v5's members: ";
    for(auto it=v5.cbegin();it!=v5.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv6's size is "<<v6.size()<<endl;
    cout<<"v6's members: ";
    for(auto it=v6.cbegin();it!=v6.cend();it++)
    cout<<*it<<"    ";

    cout<<"\nv7's size is "<<v7.size()<<endl;
    cout<<"v7's members: ";
    for(auto it=v7.cbegin();it!=v7.cend();it++)
    cout<<*it<<"    ";
    cout<<endl;

    system("pause");
    return 0;
}
//result:完全一致
3.21输出下列v的容量和内容,使用迭代器,并判断是否与自己想的一样。

 

//把第一段string改成大写
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    vector<string> v1={"MaLong NB","ChenMeng NB",""};//1、*it为迭代器所指引用;2、it->begin()=(*it).begin()。
    for(auto it=v1.begin(); it!=v1.end()&&!it->empty(); it++)
    {
        for(auto &a:*it)
        a=toupper(a);
        cout<<*it<<endl;
    }

    system("pause");
    return 0;
}
//result:MALONG NB
3.22把第一段string改成大写

 

//3.23迭代器使得数值x2
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    vector<int> v1{0,1,2,3,4,5,6,7,8,9};
    for(auto it=v1.begin();it!=v1.end();it++)
    {
        (*it)*=2;
        cout<<*it<<endl;
    }
    system("pause");
    return 0;
}
3.23迭代器使得数值x2

 

这篇关于C++primer 练习随便写写的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!