C++ primer 第三章 字符串、向量和数组 练习题

3.1节练习

练习3.1:使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习。
略。

3.2.2 节练习

练习3.2:编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。

# include <iostream>
using namespace std;
int main(){
    string line;
    while (getline(cin, line)){
        cout << line << endl;
    }
    return 0;
}
# include <iostream>
using namespace std;
int main(){
    string word;
    while (cin>>word){
        cout << word << endl;
    }
    return 0;
}

练习3.3:请说明string类的输入运算符和getline函数分别是如何处理空格字符的。
输入运算符会忽略开头的空格,从第一个真正的字符开始读起,直到遇到下一个空格或者换行,最终保存的字符串不包含最后的那个空格或者换行(最后的那个空格会被读入,但不会被保存)。如果输入流中没有任何字符全是空格,保存的就是一个空字符串。
getline函数只关注换行符,不会忽略开头和中间的空格。注意,换行符会被读入,但是不会被保存。

练习3.4:编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不相等,输出较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

# include <iostream>
using namespace std;
int main(){
    string s1, s2;
    cin >> s1 >> s2;
    if(s1==s2)  cout << "True" << endl;
    else{
        cout << "False" << endl;
        if(s1>s2)  cout << s1 << endl;
        else   cout << s2 << endl;
    }
    return 0;
}
# include <iostream>
using namespace std;
int main(){
    string s1, s2;
    cin >> s1 >> s2;
    if(s1.size()==s2.size())  cout << "True" << endl;
    else{
        cout << "False" << endl;
        if(s1.size()>s2.size())  cout << s1 << endl;
        else   cout << s2 << endl;
    }
    return 0;
}

练习3.5:编写一段程序从标准输入中读入多个字符串并把他们拼接在一起,输出拼接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。

# include <iostream>
using namespace std;
int main(){
    string s, b;
    b = "";
    while(cin >> s){     // 结束循环,windows是ctrl+z,linux是ctrl+d
        b = b + s;
    }
    cout << b << endl;
    return 0;
}
# include <iostream>
using namespace std;
int main(){
    string s, b;
    b = "";
    while(cin >> s){     // 结束循环,windows是ctrl+z,linux是ctrl+d
        b = b + s + " ";
    }
    cout << b << endl;
    return 0;
}

3.2.3节练习

练习3.6:编写一段程序,使用范围fro语句将字符串内的所有字符用x代替。

# include <iostream>
using namespace std;
int main(){
    string s1 = "dsbdijasjoi";
    for (auto &c : s1){
        c = 'x';
    } 
    cout << s1 << endl;
    return 0;
}

练习3.7: 就上一题完成的程序而言,如果将循环控制变量的类型设置为char会发生什么?先估计一下结果,再编程验证。
设置为char,原字符串不会发生改变。

# include <iostream>
using namespace std;
int main(){
    string s1 = "dsbdijasjoi";
    for (char c : s1){
        c = 'x';
    } 
    cout << s1 << endl;
    return 0;
}

练习3.8:分别用while和传统的fro循环写第一题的程序,你觉得那种形式更好呢?为什么?
用while写:

# include <iostream>
using namespace std;
int main(){
    string s1 = "dsbdijasjoi";
    decltype(s1.size()) index = 0;
    while(index<s1.size()){
        s1[index] = 'x';
        index++;
    }
    cout << s1 << endl;
    return 0;
}

类似的也能写成传统的for循环形式(非基于范围的for语句):

# include <iostream>
using namespace std;
int main(){
    string s1 = "dsbdijasjoi";
for(decltype(s1.size()) index = 0; index<s1.size(); index++ ){
        s1[index] = 'x';
    }
    cout << s1 << endl;
    return 0;
}

我觉得 基于范围的for语句>传统for循环形式>while循环形式。因为那样看起来短。。。。。

练习3.9:下面的而程序有何作用?它合法吗?如果不合法,为什么?
string s;
cout << s[0] << endl;
不合法,因为s会初始化为空字符串,s[0]超下标了,虽然我的编译器没有报错,但最好不要这样做。

练习3.10:编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。

# include <iostream>
using namespace std;
int main(){
    string s, n="";
    cin >> s;
    for(auto &c : s){
        if (!ispunct(c))   n = n + c;
    }
    cout << n << endl;
}

练习3.11:下面的范围for语句合法吗?如果合法,c的类型是什么?
const string s = “Keep out!”
for (auto &c : s) {//}
不合法,常量类型不可以被修改。

3.3.1节练习

练习3.12:下列vector对象的定义有不正确的吗?如果有请指出来。对于正确的,描述其执行结果;对于不正确的,说明其错误的原因。
vector<vector> ivec;
正确,默认初始化。
vector svec = ivec;
不正确,类型不一致的vector不能进行拷贝初始化
vector svec(10, “null”);
正确,初始化包含十个“null”的容器

练习3.13:下列的vector对象包含多少个元素?这些元素的值分别是什么?
vector v1;
0个元素
vector v2(10);
10个元素,全是0
vector v3(10, 42);
10个元素,全是42
vector v4{10};
一个元素,是10
vector v5{10, 42};
两个元素,是10和42
vector v6{10};
十个元素,全是“”(空字符串)
vector v7{10, “hi”};
十个元素,全是“hi”

3.3.2节练习

练习2.14:编写一段程序,用cin读入一组整数,并把他们存入一个vector对象。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    int a;
    vector<int> A;
    while(cin>>a){
        A.push_back(a);
    }
    return 0;
}

练习3.15:改写丄题的程序,不过这次读入的是字符串。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    string a;
    vector<string> A;
    while(cin>>a){
        A.push_back(a);
    }

    return 0;
}

3.3.3节练习

练习3.16:编写一段程序,把练习3.13中的vector对象的容器和具体内容输出出来。检查你之前的回答是否正确。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<string> v6{10};
    vector<string> v7{10, "hi"};
    cout << "v1: "; 
    for(auto i : v1)  cout << i << " ";
    cout << endl;
    cout << "v1的元素个数: " << v1.size() << endl;
    cout << "v2: "; 
    for(auto i : v2)  cout << i << " ";
    cout << endl;
    cout << "v2的元素个数: " << v6.size() << endl;
    cout << "v3: "; 
    for(auto i : v3)  cout << i << " ";
    cout << endl;
    cout << "v3的元素个数: " << v6.size() << endl;
    cout << "v4: "; 
    for(auto i : v4)  cout << i << " ";
    cout << endl;
    cout << "v4的元素个数: " << v6.size() << endl;
    cout << "v5: "; 
    for(auto i : v5)  cout << i << " ";
    cout << endl;
    cout << "v5的元素个数: " << v6.size() << endl;
    cout << "v6: ";   
    for(auto i : v6)  cout << i << " ";    //虽然输出看不到,但是真的有10个元素
    cout << endl;
    cout << "v6的元素个数: " << v6.size() << endl;
    cout << "v7: "; 
    for(auto i : v7)  cout << i << " ";
    cout << endl;
    cout << "v7的元素个数: " << v6.size() << endl;

}

练习3.17:从cin读入读入一组词,并把他们存入一个vector对象,然后设法把所有的词都改成大写形式。输出改变后的结果,每个词占一行。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    string word;
    vector<string> Word;
    while(cin >> word){
        Word.push_back(word);
    }
    for (auto &w : Word){
        for(auto &c : w){
            c = toupper(c);
        }
        cout << w << endl;
    }
    return 0;
}

练习3.18:下面的程序合法吗?如果不合法,你准备如何修改?
vector ivec;
ivec[0] = 42;

不合法,不管是字符串还是容器,都不能通过下标添加元素,只能通过下标访问和修改已经存在的元素。
修改:用push_back()添加元素即可。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<int> ivec;
    ivec.push_back(42);
    return 0;
}

练习3.19:如果想定义一个含有十个元素的vector对象,所有元素的值都是42,请列举出三种不同的实现方法。哪种方法更好呢?为什么?

    vector<int> v1(10, 42);
    vector<int> v2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
    vector<int> v3 = {42, 42, 42, 42, 42, 42, 42, 42, 42, 42};

显然第一种最好

练习3.20:读入一组整数并把他们存入一个vector对象,将每对相邻整数的和输出出来。改写你的程序,这次要求输出第1个和最后1个元素的和,接着输出第2个和倒数第2个元素的和,以此类推。

# include <iostream>
# include <vector>

using namespace std;

int main(){
    vector<int> num;
    int a;
    while (cin >> a){
        num.push_back(a);
    }
    string::size_type len = num.size();
    cout << "有" << len << "个数字" << endl;

    for (string::size_type index = 0; index < len-1; index++){
        cout << num[index]+num[index+1] << endl;
    }
    return 0;
}
# include <iostream>
# include <vector>

using namespace std;

int main(){
    vector<int> num;
    int a;
    while (cin >> a){
        num.push_back(a);
    }
    string::size_type len = num.size();
    cout << "有" << len << "个数字" << endl;
    // 如果是偶数个就正常加,如果是奇数个就把中间那个自己和自己加
    for (string::size_type index = 0; index <= len/2-1; index++){
        cout << num[index]+num[len-index-1] << endl;
    }
    if (len%2!=0)   cout << num[len/2]*2 << endl;
    return 0;
}

3.4.1节练习

练习3.21:请使用迭代器重做3.3.3节(第94页)的第一个练习。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<string> v6{10};
    vector<string> v7{10, "hi"};
    cout << "v1: "; 
    for(auto it = v1.begin(); it != v1.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v1的元素个数: " << v1.size() << endl;
    cout << "v2: "; 
    for(auto it = v2.begin(); it != v2.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v2的元素个数: " << v6.size() << endl;
    cout << "v3: "; 
    for(auto it = v3.begin(); it != v3.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v3的元素个数: " << v6.size() << endl;
    cout << "v4: "; 
    for(auto it = v4.begin(); it != v4.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v4的元素个数: " << v6.size() << endl;
    cout << "v5: "; 
    for(auto it = v5.begin(); it != v5.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v5的元素个数: " << v6.size() << endl;
    cout << "v6: ";   
    for(auto it = v6.begin(); it != v6.end(); ++it)  cout << *it << " ";   //虽然输出看不到,但是真的有10个元素
    cout << endl;
    cout << "v6的元素个数: " << v6.size() << endl;
    cout << "v7: "; 
    for(auto it = v7.begin(); it != v7.end(); ++it)  cout << *it << " ";
    cout << endl;
    cout << "v7的元素个数: " << v6.size() << endl;

}

练习3.22:修改之前的那个输出text第一段的程序,首先把text的第一段全都改成大写形式,然后再输出它。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<string> text = {"I love you!", "yes, You love me.", "", "what????"};
    for (auto it = text.begin(); it != text.end() && ! it->empty(); ++it){
        for (auto &c : *it){
            if(!isspace(c)){
                c = toupper(c);
            }
        }
        cout << *it << " ";    // 输出第一段
    }
    cout << endl;
    for (auto it = text.begin(); it != text.end(); ++it){
        cout << *it << " ";    // 输出全部的看一看
    }
    cout << endl;
    return 0;

}

练习3.23:编写一段程序,创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容,检查程序是否正确。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<int> shu = {2, 5, 9, 42, 3, 62, 12, 4, 1, 52};
    for (auto it = shu.begin(); it != shu.end(); ++it){
        *it  = *it * 2;
        cout << *it << " ";

    }
    cout << endl;
    return 0;
}

3.4.2节练习

练习3.24:请使用迭代器重做3.3.3节(第94页)的最后一个练习?

# include <iostream>
# include <vector>

using namespace std;

int main(){
    vector<int> num;
    int a;
    while (cin >> a){
        num.push_back(a);
    }
    string::size_type len = num.size();
    cout << "有" << len << "个数字" << endl;

    for (auto index = num.begin(); index != --num.end(); ++index){
        cout << *index + *(index+1) << endl;
    }
    
    return 0;
}
# include <iostream>
# include <vector>

using namespace std;

int main(){
    vector<int> num;
    int a;
    while (cin >> a){
        num.push_back(a);
    }
    string::size_type len = num.size();
    cout << "有" << len << "个数字" << endl;
    // 如果是偶数个就正常加,如果是奇数个就把中间那个自己和自己加
    auto mid = num.begin() + num.size()/2;
    if(len%2 != 0){
        int i = 1;
        for(auto index = num.begin(); index < mid; ++index){
            cout << *index + *(num.end()-i) << endl;
            i++;
        }
        cout << 2* *mid << endl;
    }
    else{
        int i = 1;
        for(auto index = num.begin(); index < mid; ++index){
            cout << *index + *(num.end()-i) << endl;
            i++;
        }
    }
    return 0;
}

练习3.25:3.3.3节(第93页)划分分数的程序是使用下标运算符实现的,请利用迭代器改写该程序并实现完全相同的功能。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    vector<unsigned> scores(11, 0);
    unsigned grade;
    while(cin >> grade){
        if (grade <= 100){
            *(scores.begin() + grade/10) = *(scores.begin() + grade/10) + 1;
        }
    }
    for(auto index = scores.begin(); index != scores.end(); index++){
        cout << *index << " ";
    }
    cout << endl;
    return 0;
}

练习3.26:在100页的二分查找搜索程序中,为什么用的是mid = beg + (end - beg) / 2,而非mid = (beg + end) / 2; ?
我觉得原因是,相加可能会导致溢出,而相减永远不会。

3.5.1节练习

练习3.27:假设txt_size是一个无参数的函数,它的返回值是int。请回答下列哪个定义是非法的?为什么?
unsigned buf_size = 1024;
(a) int ia[buf_size];
不合法,数组的维度必须是一个常量表达式
(b) int ia[4*7-14];
不合法,数组的维度必须是一个常量表达式
© int ia[txt_size()];
不合法,数组的维度必须是一个常量表达式,只有当函数返回类型是constexpr时才可以
(d) char st[11] = “fundamental”;
不合法,字面值字符串后面还有一个空字符,这个空字符也需要一个数组位去放,所以至少需要一个长度为12的数组去存放

练习3.28:下列数组中元素的值是什么?
string sa[10]; //十个空串
int ia[10]; //十个0
int main(){
string sa2[10]; //十个空串
int ia2[10]; //十个0
}

练习3.29: 相比于vector来说,数组有哪些缺点,请列举一些。
数组的大小确定不变,不能随意向数组中增加元素。

3.5.2节练习

练习3.30:指出下列代码中的索引错误。
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix)
ia[ix] = ix;

数组的下标是0-size-1,而不是1- size,代码中的下标ix=size时会出现下标越界。

练习3.31:编写一段程序,定义一个含有10个int的数组,令每个元素的值就是其下标值。

# include <iostream>
# include <vector>
using namespace std;
int main(){
	int a[10];
    for(int index=0; index < 10; ++index)
        a[index] = index;
    for(auto i : a)
        cout << i << " ";
    cout << endl;
    return 0;
}

练习3.32:将上一题刚刚创建的数组拷贝给另一个数组。利用vector重写程序,实现类似的功能。

# include <iostream>
# include <vector>
using namespace std;
int main(){
	int a[10];
    for(int index=0; index < 10; ++index)
        a[index] = index;
    for(auto i : a)
        cout << i << " ";
    cout << endl;
    int b[10];
    for(int index=0; index < 10; ++index)
        b[index] = a[index];
    for(auto i : b)
        cout << i << " ";
    cout << endl;
    return 0;
}
# include <iostream>
# include <vector>
using namespace std;
int main(){
	vector<int> a;
    for (int index = 0; index < 10; index++){
        a.push_back(index);
    }
    for(auto i : a){
        cout << i << " ";
    }
    cout << endl;
    vector<int> b = a;
    for(auto i : b){
        cout << i << " ";
    }
    cout << endl;
}

练习3.33:对于104页的程序来说,如果不初始化scores将会发生什么?
不初始化不能得到正确的结果,因为数组中的初始值是随机的。

3.5.3节练习

练习3.34:假定p1和p2指向同一个数组中的元素,则下面程序的功能是什么?什么情况下该程序是非法的?
p1 += p2 - p1;
该程序的功能是让p1指针指向和p2指针同样的元素。当p1指针为常量类型,不可更改时是非法的。
练习3.35:编写一段程序,利用指针将数组中的元素置为0。

# include <iterator>
using namespace std;
int main(){
    int a[] = {1, 2, 3, 4};
    int *p = &a[0];
    while(p < end(a)){
        *p = 0;
        p++;
    }
    return 0;
}

练习3.36:编写一段程序,比较两个数组是否相等。再写一段程序,比较两个vector对象是否相等。

# include <iterator>
# include <vector>
# include <iostream>
# include <cstddef>
using namespace std;
int main(){
    vector<string> s1{"hi", "ha2o", "wang", "xiaobo"};
    vector<string> s2{"hi", "hao", "wang", "xiaobo"};
    // vector可以直接比较是否相等,甚至可以比较大小
    if(s1 == s2){
        cout << "vector相等" << endl;
    }
    else
        cout << "vector不相等" << endl;
    int a1[] = {0, 1, 2, 3};
    int a2[] = {0, 1, 2, 3};
    // 内置数组不能直接比较大小需要先比较长度再挨个儿比较
    if(sizeof(a1)/sizeof(a1[0]) != sizeof(a2)/sizeof(a2[0])){
        cout << "数组不相等" << endl;
    }
    else{
        int *p1 = a1;
        int *p2 = a2;
        while(p1 < end(a1)){
            if(*p1 != *p2){
                cout << "数组不相等" << endl;
                break;
            }
            else{
                p1++; p2++;
            }
        }
        if(p1 == end(a1))
            cout << "数组相等" << endl;
    }
    

    return 0;
}

3.5.4节练习

练习3.37:下面的程序是何含义,程序的输出结果是什么?

	const char ca[] = {'h', 'e', 'l', 'l', 'o'};
    const char *cp = ca;
    while(*cp){
        cout << *cp << endl;
        ++cp;
    }

这个程序的意思是输出从ca首地址开始的元素,用换行符隔开,直到遇见空字符‘\0’才会停止。但是我在ubuntu系统上试,输出就是hello,不知道问什么。
在这里插入图片描述
练习3.38:在本节中我们提到,两个指针相加不但是非法的,而且也没有什么意义。请问为什么两个指针相加没什么意义?
指针中存放的是地址,两个地址相加,可能会变成一个很大的值,就算有这个地址存在,也不知道指向的是什么内容,所以没有什么意义。

练习3.39:编写一段程序,比较两个string对象。再编写一段程序,比较两个C风格字符串的内容。

# include <iostream>
# include <cstring>
using namespace std;
int main(){
    string s1 = "sadasdas";
    string s2 = "asdmasmd";
    if(s1 == s2){
        cout << "两个string相等" << endl;
    }
    else
        cout << "两个string不相等" << endl;
    // char c1[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    // char c2[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    char c1[] = "hello";
    char c2[] = "hello";
    if(strcmp(c1, c2) == 0)
        cout << "两个C风格字符串相等" << endl;
    else
        cout << "两个C风格字符串不相等" << endl;
    

    return 0;
}
}

练习3.40:编写一段程序,定义两个字符数组并用字符串字面值初始化它们;接着再定义一个字符数组存放前两个数组连接后的结果。使用strcpy和strcat把前面两个数组的内容拷贝到第三个数组中。

# include <iostream>
# include <cstring>
using namespace std;
int main(){
    char c1[100] = "ni hao,";
    char c2[] = "wang xiaobao";
    char c3[100];
    strcpy(c3, strcat(c1, c2));
    char *p = c3;
    while(*p != '\0'){
        cout << *p;
        p++;
    }
    cout << endl;
    return 0;
}

3.5.5节练习

练习3.41:编写一段程序,用整型数组初始化一个vector对象。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    int a[] = {1, 2, 3, 4};
    vector<int> A(a, a+3);
    for(auto p : A){
        cout << p << " ";
    }   
    cout <<  endl;
    return 0;
}

练习3.42:编写一段程序,将含有整数元素的vector对象拷贝给一个整型数组。

# include <iostream>
# include <vector>
using namespace std;
int main(){
    int a[] = {1, 2, 3, 4};
    vector<int> A;
    // 只能挨个儿拷贝
    for(auto p = a; p != end(a); p++){
        A.push_back(*p);
    }   
    for(auto i : A){
        cout << i << " ";
    }
    cout <<  endl;
    return 0;
}

3.6节练习

练习3.43:编写3个不同版本的程序,令其均能输出ia的元素。版本1使用范围for语句管理迭代过程;版本2和版本3都使用普通的for语句。其中版本2要求用下标运算符,版本3要求用指针。此外,在所有3个版本的程序中都要直接写出数据类型,而不能使用类型别名、auto关键字或decltype关键字。

# include <iostream>

using namespace std;
int main(){
    int a[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    // 范围for语句
    cout << "范围for语句:" << endl; 
    for( int (&row)[4] : a){
        for(int &col : row){
            cout << col << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "下标运算符:" << endl; 
    for(size_t row=0; row < 3; row++){
        for(size_t col=0; col < 4; col++){
            cout << a[row][col] << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "指针:" << endl; 
    for(int (*row)[4] = a; row != a + 3; row++){
        for(int *col = *row; col != *row + 4; col++){
            cout << *col << " ";
        }
        cout << endl;
    }
    return 0;
}

练习3.44:改写上一个练习中的程序,使用类型别名来代替循环控制变量的类型。

# include <iostream>

using namespace std;
int main(){
    int a[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    // 范围for语句
    cout << "范围for语句:" << endl; 
    using int_array = int[4];
    for( int_array (&row) : a){
        for(int &col : row){
            cout << col << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "下标运算符:" << endl; 
    for(size_t row=0; row < 3; row++){
        for(size_t col=0; col < 4; col++){
            cout << a[row][col] << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "指针:" << endl; 
    for(int_array (*row) = a; row != a + 3; row++){
        for(int *col = *row; col != *row + 4; col++){
            cout << *col << " ";
        }
        cout << endl;
    }
    return 0;
}

练习3.45:再一次改写程序,这次使用auto关键字。

# include <iostream>

using namespace std;
int main(){
    int a[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    // 范围for语句
    cout << "范围for语句:" << endl; 
    for(auto &row : a){
        for(auto &col : row){
            cout << col << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "下标运算符:" << endl; 
    for(auto row=0; row < 3; row++){
        for(auto col=0; col < 4; col++){
            cout << a[row][col] << " ";
        }
        cout << endl;
    }
    cout << "************************" << endl;

    cout << "指针:" << endl; 
    for(auto row = a; row != a + 3; row++){
        for(auto col = *row; col != *row + 4; col++){
            cout << *col << " ";
        }
        cout << endl;
    }
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值