//exercise 6.17
#include <iostream>
#include <string>
using namespace std;
bool exist_upper(const string &s)
{
for(auto id : s)
{
if(isupper(id))
return true;
}
return false;
}
void string_tolower(string &s)
{
if(exist_upper(s))
{
for(auto &c : s)
c = tolower(c);
}
}
int main() {
// your code goes here
string s = "Hello World";
cout << exist_upper(s) << endl;
string_tolower(s);
cout << s <<endl;
return 0;
}
//exercise 6.27
#include <iostream>
using namespace std;
int sum(initializer_list<int> val)
{
int result = 0;
for(auto p = val.begin(); p != val.end(); ++p)
result += *p;
return result;
}
int main() {
// your code goes here
cout << sum({1, 2, 3, 4, 5});//一定不能忘记大括号!
return 0;
}
//exercise 6.55
#include <iostream>
#include <vector>
using namespace std;
int func_add(int a, int b)
{
return a + b;
}
int func_sub(int a, int b)
{
return a - b;
}
int func_div(int a, int b)
{
return a / b;
}
int func_mul(int a, int b)
{
return a * b;
}
using pf = int (*)(int, int);
vector<pf> v{func_add, func_sub, func_div, func_mul};
int main()
{
for (auto it = v.cbegin(); it != v.cend(); ++it)
cout << (*it)(2, 2) << std::endl;
return 0;
}