Example 1:
输入一行字符的集合,遇到换行符结束输入,并且判断这个字符串对象中的大写、小写、空格、数字、以及其他符号个数。
Code:
// #include<cctype> using namespace std; int main() { //统计输入的字符串中大写字母、小写字母、空格、数字、以及其他符号的数量 string str; int upper_n{0},num_n{0},lower_n{0},space_n{0},other_n{0}; cout<<"请输入你想统计的字符:(遇到换行符跳出)"<<endl; getline(cin,str); //进行判断 /*c++11标准提供一种语句:范围for (range for statement) 格式如下: for(declaration : expression) { statement; } 定义一个变量,然后这个变量去遍历一个对象,然后对每次遍历的对象进行操作。 */ for(auto c:str) { if(isdigit(c)) num_n++; else if(islower(c)) lower_n++; else if(isupper(c)) upper_n++; else if(isspace(c)) graph_n++; else other_n++; } cout<<"小写字母个数:"<<lower_n<<endl <<"大写字母个数:"<<upper_n<<endl <<"数 字 个 数:"<<num_n<<endl <<"空 格 个 数:"<<space_n<<endl <<"其他字符个数:"<<other_n<<endl; return 0; }
Run:
xiandonghua@No:~/c++/c++bin$ ./built_in_type 请输入你想统计的字符:(遇到换行符跳出) ABCDEFGH -- abcdefgh @@@%%%% 12345678 小写字母个数:8 大写字母个数:8 数 字 个 数:8 空 格 个 数:6 其他字符个数:9
以上结果让我产生了新的疑问,就是我并没有调用cctype这个库,为什么可以正常调用{isupper(),isspace(),islower()},然后查了度娘:
有些观点是:可能在其它 我导入的库中,有调用cctype。
然后我查看了我的iostream文档:
#ifndef _GLIBCXX_IOSTREAM #define _GLIBCXX_IOSTREAM 1 #pragma GCC system_header #include <bits/c++config.h> #include <ostream> #include <istream> namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @name Standard Stream Objects * * The <iostream> header declares the eight <em>standard stream * objects</em>. For other declarations, see * http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html * and the @link iosfwd I/O forward declarations @endlink * * They are required by default to cooperate with the global C * library's @c FILE streams, and to be available during program * startup and termination. For more information, see the section of the * manual linked to above. */ //@{ extern istream cin; /// Linked to standard input extern ostream cout; /// Linked to standard output extern ostream cerr; /// Linked to standard error (unbuffered) extern ostream clog; /// Linked to standard error (buffered) #ifdef _GLIBCXX_USE_WCHAR_T extern wistream wcin; /// Linked to standard input extern wostream wcout; /// Linked to standard output extern wostream wcerr; /// Linked to standard error (unbuffered) extern wostream wclog; /// Linked to standard error (buffered) #endif //@} // For construction of filebuffers for cout, cin, cerr, clog et. al. static ios_base::Init __ioinit; _GLIBCXX_END_NAMESPACE_VERSION } // namespace #endif /* _GLIBCXX_IOSTREAM */
此时的我有点懵,然后我看到一个我不认识的头文件bits/c++config.h,但很不幸运,我没有在/usr/include/bits
里找到它,我在/usr/include/x86_64-linux-gnu/c++/5.4.0/bits$
找到了。
这个问题我查了一段时间,没有结果,总之的出一个结论,不管有没有调用这个头文件,他都能运行成功,但最好的情况下还是调用该有的头文件,防止没有其它头文件未包含该头文件。
也希望如有大佬看到我这个遗留的问题,请告知一二,谢谢!!!
Vector 模板:
定义和初始化vector
vector |
---|
vector<T> var_1 是一个空类型vector |
vector<T> var_2(var_1) var_2中包含所有var_1中的元素 |
vector<T> var_2=var_1 var_2中包含所有var_1中的元素 |
vector<T> var_3(n,var) var_3中包含n个var |
vector<T> var_4(n) var_4包含n个初始化值 |
vector<T> var_5{1,2,3,4,5.....} 初始var_5的个数 |
vector<T>var_5={1,2,3,4,5....} 等价上一个 |
vector 是一个动态数组 |
其实看到上述表中,我对于vector<T>var_4(n
,比较好奇,所以我就写了如下代码,来查看它到底是怎样初始化的。
Example 2:
//部分代码 int n=10; vector<int> age(n); for(auto i : age) { cout<<i<<endl; }
Run:
xiandonghua@No:~/c++/c++bin$ ./built_in_type 0 0 0 0 0 0 0 0 0 0
我想如果运用vector<T> var_4(n)这样的形式初始化的话,其中var_4中的吃初始个数是n,初始值由系统自动初始化值。
如何对于vector对象进行操作呢?
push_back() and pop_back()
Example 3:
//部分代码 vector<string> students_name; string name; cout<<"请输入学生姓名:(quit推出)"<<endl; while(true) { cin>>name; if(name=="quit") break; students_name.push_back(name); } cout<<"你们班同学名单:"<<endl; for(auto i : students_name) { cout<<i<<endl; } students_name.pop_back(); cout<<"你们班删除最后一个同学的名单:"<<endl; for(auto i : students_name) { cout<<i<<endl; }
Run:
xiandonghua@No:~/c++/c++bin$ ./built_in_type 请输入学生姓名:(quit推出) bob peter alice quit 你们班同学名单: bob peter alice 你们班删除最后一个同学的名单: bob peter
这样可以看出,vector是一个动态数组,也可以说是一个容器。
而push_back和pop_back 的作用分别就是:
相当于把一个数据放入一个按照顺序放入一个盒子里,这个盒子里有很多个格子并且是有顺序的。
这些数据中的每一部分按照我们给定的规则去放入格子里。
而push_back()的作用,就是把单个数据压在盒子最里面的那个格子里。
pop_back(),就是我们把数据都存储好了,它去掉这个盒子里的最外面的那个数据。
一步一步慢慢学,不怕学的比别人慢,就怕什么也没学懂。
----至自己