C++ Primer第四版习题--4.26

#include <iostream>
#include <cstring>

int main()
{
    const int buf_size = 1024;
    char *str1, *str2;
    str1 = new char[buf_size];
    str2 = new char[buf_size];
    std::cin >> str1 >> str2;
    int result;
    result = strcmp(str1, str2);
    if(result>0)
        std::cout << "str1 is bigger!\n";
    else if(result<0)
        std::cout << "str2 is bigger!\n";
    else
        std::cout << "equal!\n";
    delete [] str1;
    delete [] str2;
    return 0;
}

如果编译器报错

error: ‘strcmp‘ was not declared in this scope

     result = strcmp(str1, str2);

                               ^

需要添加头文件<cstring>

时间: 2024-10-04 01:55:49

C++ Primer第四版习题--4.26的相关文章

C++ Primer第四版习题--5.18

#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string*> spvec; string str; while(cin>>str) { string *sp = new string; *sp = str; spvec.push_back(sp); } vector<string*>::

C++ Primer第四版习题--3.18

#include <iostream> #include <vector> //#include <cctype> int main() { std::vector<int> ivec(10, 0); int num; for(std::vector<int>::iterator iter=ivec.begin(); iter!=ivec.end(); ++iter) { std::cin >> num; *iter=2*num; s

C++ Primer第四版习题--3.8

#include <iostream> #include <string> int main() { std::string str, str_long; std::cin>>str_long; while(std::cin>>str) str_long = str_long + ' ' + str; std::cout << str_long << std::endl; return 0; }

C++ Primer第四版习题--3.10

#include <iostream> #include <string> #include <cctype> int main() { std::string str, result_str; bool flag=false; while(getline(std::cin, str)) { for(unsigned index=0; index<str.size(); ++index) { char ch; ch = str[index]; if(ispunct

C++ Primer第四版习题--4.28

#include <iostream> #include <vector> int main() { std::vector<int> ivec; int temp; while(std::cin>>temp) ivec.push_back(temp); int *cp =new int[ivec.size()]; for(std::vector<int>::iterator iter=ivec.begin(); iter!=ivec.end()

C++ Primer第四版习题--4.30

#include <iostream> #include <cstring> int main() { const char *cp1 = "Hello!"; const char *cp2 = "How are you."; size_t len = strlen(cp1) + strlen(cp2); char *result = new char[len]; strcpy(result, cp1); strcat(result, cp2

C++ Primer第四版习题--3.13

//读一组数到vector对象,计算收尾配对元素的和并输出 #include <iostream> #include <vector> int main() { std::vector<int> ivec; int temp, add; while(std::cin>>temp) ivec.push_back(temp); typedef std::vector<int>::size_type vec_type; vec_type flag; f

C++ Primer第四版习题--3.14

/*读入一段文本到vector对象,每个单词存储为vector中的一个元素, * 把vector中的每个单词转化为大写字母. * 输出转化后的元素,每8个单词为一行 **/ #include <iostream> #include <vector> #include <string> #include <cctype> int main() { std::vector<std::string> svec; std::string word; wh

C++ Primer第四版习题--3.17(重做3.14)

/*读入一段文本到vector对象,每个单词存储为vector中的一个元素, * 要求用迭代器实现,把vector中的每个单词转化为大写字母. * 输出转化后的元素,每8个单词为一行 **/ #include <iostream> #include <vector> #include <string> #include <cctype> int main() { std::vector<std::string> svec; std::string