C++学习

#include<iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
void swap(string, string);
void main() {

    /*对象作为函数参数*/
    string strnow("now"), strago("ago");
    swap(strnow, strago);
    cout << "返回后:strnow=" << strnow << " strago=" << strago << endl;

    system("pause");
}
void swap(string s1,string s2){
    string temp = s1; s1 = s2; s2 = temp;//temp作为一个中间值变量赋值
    cout << "交换为:strnow=" << s1 << " strago=" << s2 << endl;

}/*
   交换为:strnow=ago strago=now
   返回后:strnow=now strago=ago
请按任意键继续. . .*/

 

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef double m[10];
void main() {
    m a = { 12,34,56,78,90,98,76,85,64,43 };
    m &b = a;
    a[2] = 100;
    for (int i = 0; i < 5; i++)cout << b[i] << " ";//b是a的引用,即b[i]==a[i],调用数组a的每个元素,并替换“56”

    system("pause");//让程序运行完后,停止在当前页面,而不自动退出
}

 

#include<iostream>
#include<stdlib.h>
#include<algorithm>  //数组的升幂排序、反转、复制等
#include<functional> //对数组降幂排序和检索需要包含头文件
#include <iterator>    //std:ostream_iterator
using namespace std;


void main() {
    double a[] = { 19.1,120.3,12.2,34.4,56.5 }, b[8] = {8};//让数组b存储8个元素,{},8为第一个元素,其他无说明则问0
    copy(b, b + 8, ostream_iterator<double>(cout, " ")); cout << endl;
    copy(a + 2, a + 4, ostream_iterator<double>(cout, " ")); cout << endl;
    reverse_copy(a+1,a+4, ostream_iterator<double>(cout, " ")); cout << endl;
    copy(a, a + 4, &b[4]);//
    copy(b, b +8, ostream_iterator<double>(cout, " ")); cout << endl;
    sort(a + 1, a + 3);
    copy(a , a + 4, ostream_iterator<double>(cout, " ")); cout << endl;
    sort(b + 1, b + 6);
    copy(b, b + 8, ostream_iterator<double>(cout, " ")); cout << endl;
    /*输出如下:
        8 0 0 0 0 0 0 0
        120.3 34.4
        34.4 120.3 12.2
        8 0 0 0 19.1 12.2 120.3 34.4
        19.1 12.2 120.3 34.4
        8 0 0 0 12.2 19.1 120.3 34.4  */
                
    system("pause");
}

 

#include<iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
void main() {
    complex <int> num1(2, 3);
    complex <float> num2(3.5, 4.5);
    string str1("real is");
    string str2 = "image is";
    cout << str1 << num1.real() << ',' << str2 << num1.imag() << endl;
    cout << str1 << num2.real() << ',' << str2 << num2.imag() << endl;

/*string obj*/
    string str11 = "we are here!", str22 = "aaaaaaaaaaaa";/*相当给str22存放一个空间存储,如果为空是没有空间来存储的*/
    reverse(&str11[0], &str11[0] + 12);
    cout <<"str11:"<< str11 << endl;//!ereh era ew
    copy(&str11[0], &str11[0] + 12,&str22[0]);
    cout << str22 << endl;//!ereh era ew
    reverse_copy(&str22[0], &str22[0] + 12, ostream_iterator<char>(cout));//we are here!
/*string obj*/
    string str_a = "wearehere!", str_b(str_a);//str_b(str_a),str_b拥有str_a的存储空间
    reverse(str_a.begin(), str_a.end());
    cout <<"str_a*"<< str_a << endl;// !ereheraew
    copy(str_a.begin(), str_a.end(), str_b.begin());
    sort(str_a.begin(), str_a.end());
    cout << "str_b*" << str_b << endl;
    cout << "str_a**" << str_a << endl;
    reverse_copy(str_a.begin(), str_a.end(), str_b.begin());
    cout << str_b << endl;
    reverse(str_b.begin()+2, str_b.begin()+7);/*str_b.begin()+7是在开始位置的数起第七个位置作为翻转的结束*/
    copy(str_b.begin() + 2, str_b.begin() + 7, ostream_iterator<char>(cout));
    cout << endl;
    sort(str_a.begin(), str_a.end(), greater<char>());//排序降幂
    cout << str_a << endl;
    str_b.swap(str_a);
    cout << str_b <<"*****"<< str_a<< endl;
    /*  real is2, image is3
        real is3.5, image is4.5
        str11: !ereh era ew
        !ereh era ew
        we are here!
        str_a * !ereheraew
        str_b * !ereheraew
        str_a** !aeeeehrrw
                wrrheeeea!
        eeehr
        wrrheeeea!
        请按任意键继续. . .*/
    
    system("pause");
}

 

#include<iostream>
#include<string>

using namespace std;
class test{
private:
    int n;
public:
    test(int i) { n = i; cout << "构造函数:" << i << endl; }
    ~ test() { cout << "析构函数:" << n << endl; }
    int getn() { return n; }
    void inc() { ++n; }

};
void main() {
    cout << "开始循环:" << endl;
    for (int i = 0; i < 3; i++) {
        static test a(3);
        test b(6);
        a.inc();
        b.inc();
        cout << "a.n= " << a.getn() << endl;
        cout << "b.n= " << b.getn() << endl;

    }
    cout << "结束循环" << endl;
    cout << "退出 main()" << endl;
    system("pause");
}    

 //出圈游戏

#include<iostream>
#include<vector>

using namespace std;
class SetList{
    char name[10];
public:
    void DispName() { cout << name; }
    void SetName(char b[]) { strcpy_s(name, b); }
    void Joseph(vector<SetList>&);
};
void SetList::Joseph(vector<SetList>&c) {
    int m, start, i, j, L;
    cout << "输入间隔m(m<=20):"; cin >> m;
    while (m > 20) { cout << "重新输入m"; cin >> m; }

    cout << "从第几个人开始报数(不能大于" << c.size() << "):"; cin >> start;
    while (start > c.size()) { cout << "重新输入:"; cin >> start; }

    cout << "开始输入名字"<<endl; getchar();
    char n[10];
    for (i = 0; i < c.size(); i++) { cout << "" << i + 1 << "个人的名字:"; gets_s(n); c[i].SetName(n); }
    
    i = start - 2;//本来是减去1就可以了,但是erase(iterator it)要求里面iterator it也就是泛型指针名所指向的对象,结合p的指针名,重构一个p+i的指针名,也就是当前圈出的对象。。。erase(iterator it)向量中删除对象的方法
    vector<SetList>::iterator p; 
    p = c.begin();
    int lenth = c.size();
    for (L = 1; L <= lenth; L++) {
        j = 0;
        while (j < m) { i++; if (i == c.size())i = 0; j++; }
        if (L == lenth)break;
        c[i].DispName(); cout << ",";
        c.erase(p+i); --i;
    }
    c[i].DispName(); cout << endl;}

void main() {
    int lenth = 0;cout << "请输入人数:";
    cin >> lenth; vector<SetList>c(lenth);
    SetList game; game.Joseph(c);
    

    
    system("pause");

}

 

posted @ 2019-03-18 11:49  txurroke  阅读(137)  评论(0编辑  收藏  举报