ps:本文内的程序均为c++ 11 标准,codeblocks13.12
欢迎大家评论拍砖,消灭0回复啊,求交流,求回复,求赞,
http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read?page=1&tab=votes#tab-top
据说要成为牛逼的coder要看这些,感觉。。。
在用while循环输入时,按下CTRL+Z回车即可停止输入,新技能get
vector是一个类模板,常被称为容器(container)
3.17
#include<cctype>
using namespace std;
int main()
{
string in;
vector<string> vstr;
while(cin>>in && in!= "#")
{
vstr.push_back(in);
}
if(!vstr.empty())
{
string str;
for(auto &c: vstr)
{
for(auto &ch:c) //在单词里面单个字符大写
{
ch = toupper(ch);
}
cout<< c<<endl;
}
}
return 0;
}
3.19
#include <iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
vector<int> ivec(10,42);
vector<int> ivec2{42,42,42,42,42,42,42,42,42,42};
vector<int> ivec3(10);
for(auto &c: ivec3)
{
c=42;
}
// ivec[0] = 42;
return 0;
}
当然第一种好,简单粗暴
3.20
#include <iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
vector<int> v1;
int num;
while(cin>>num && num!=0)
{
v1.push_back(num);
}
if(!v1.empty())
{
for(decltype(v1.size()) i =0,j = v1.size()-1;i <= j;i++,j--)
{
cout<<v1[i] +v1[j]<< endl;
}
}
return 0;
}
3.22 被注释掉的是老版本的个人觉得没有for好用
这个程序调试的时候废了好大劲,现在看来是当时脑残了,果断放了两天来写,一会儿就好了,哈哈哈哈
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
vector<string> vstr;
string str;
cout<< "enter text(CTRL +Z to end):"<< endl;
while(getline(cin,str))
{
vstr.push_back(str);
}
if(vstr.size() ==0)
{
cout << "no strings there"<< endl;
return -1;
}
cout <<"##########"<<"then topper"<<endl;
for(auto it= vstr.begin();it != vstr.end() && !it->empty(); ++it)
{
/*for(string::size_type index= 0;index != (*it).size();
++index)
{
(*it)[index] = toupper((*it)[index]);
}*/
for(auto &itor: (*it)) //(*it)返回第一个string的引用,所以我用了引用的引用
{
itor = toupper(itor);
}
}
for(auto ch:vstr)
{
cout<< ch<<endl;
}
}