1.4.1
#include
using std::cout;
using std::endl;
int main(){
int sum = 0;
for(int val = 50; val <=100; ++val){
sum += val;
}
cout << "sum of 50 to 100 inclusive is "
<< sum << endl;
return 0;
}
#include
using std::cout;
using std::endl;
int main(){
for(int val = 10; val >= 0; --val){
cout << "val = " << val << endl;
}
return 0;
}
#include
using std::cout;
using std::endl;
using std::cin;
int main(){
cout << "Enter two number: " << endl;
int v = 0, v1 = 0;
std::cin >> v >> v1;
if(v <= v1){
for(; v <= v1; ++v){
cout << v << endl;
}
}
else{
for(; v1 <= v; ++v1){
cout << v1 << endl;
}
}
}
2.6.2
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sale_Data
{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
// for(Sales_Data sd; cin >> sd; cout << sd << endl);
Sales_Data book;
double price;
cin >> book.bookNo >> book.units_sold >> price >> endl;
book.revenue = book.units_sold * price;
cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price << endl;
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::cerr;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data book, book2;
double price;
cin >> book.bookNo >> book.units_sold >> price >> endl;
cin >> book2.bookNo >> book2.units_sold >> price >> endl;
if(book.bookNo == book2.bookNo){
cout << book.units_sold + book2.units_sold << endl;
}else{
cerr << "输入有误" << endl;
}
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data total_book;
double total_price;
if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
// int cnt = 1;
total_book.revenue = total_book.units_sold * total_price;
Sales_Data book;
double book_price;
while(cin >> book.bookNo >> book.units_sold >> book_price){
book.revenue = book.units_sold * book_price;
if(total_book.bookNo == book.bookNo){
total_book.units_sold += book.units_sold;
total_book.revenue += book.revenue;
// ++cnt;
}else{
cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
// std::cout << total_book.bookNo << "has " << cnt << "recordings!";
total_book.bookNo = book.bookNo;
total_price = book_price;
total_book.units_sold = book.units_sold;
total_book.revenue = book.revenue;
}
}
cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
// std::cout << total_book.bookNo << "has " << cnt << "recordings!";
}
else{
cout << " No Data" << endl;
return -1;
}
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data total_book;
double total_price;
if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
int cnt = 1;
total_book.revenue = total_book.units_sold * total_price;
Sales_Data book;
double book_price;
while(cin >> book.bookNo >> book.units_sold >> book_price){
book.revenue = book.units_sold * book_price;
if(total_book.bookNo == book.bookNo){
// total_book.units_sold += book.units_sold;
// total_book.revenue += book.revenue;
++cnt;
}else{
cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
total_book.bookNo = book.bookNo;
total_price = book_price;
total_book.units_sold = book.units_sold;
total_book.revenue = book.revenue;
}
}
cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
}
return 0;
}
一次一行
int main(){
string line;
cout << "请输入:" << endl;
while(getline(cin, line)){//以空格为区分
cout << "a" << line << endl;
}
return 0;
}
一次一个词
int main(){
string word;
cout << "请输入:" << endl;
while(cin >> word){//以空格为区分
cout << word << endl;
}
return 0;
}
#include
using namespace std;
int main(){
string s1, s2;
cin >> s1 >> s2;
if(s1 == s2){
cout << "Equal string!" << endl;
}else{
string s = s1 > s2 ? s1 : s2;
cout << s << endl;
}
}
#include
using namespace std;
int main(){
string s1, s2;
cin >> s1 >> s2;
if(s1.size() == s2.size()){
cout << "same length!" << endl;
}else{
string s = s1.size() > s2.size() ? s1 : s2;
cout << s << endl;
}
}
#include
using namespace std;
int main(){
string s, curr_s;
cout << "请输入: " << endl;
while(cin >> curr_s){
//s += ' ';
s += curr_s;
}
cout << s << endl;
}
空格分割
#include
using namespace std;
int main(){
string s, curr_s;
cout << "请输入: " << endl;
while(cin >> curr_s){
s += ' ';
s += curr_s;
}
cout << s << endl;
}
#include
#include
using namespace std;
int main(){
string s("Hello World");
for(auto &c : s){
c = 'X';
}
cout << " " << s << endl;
return 0;
}
不用引用最后的字符串还是原样输出。
while 循环
int main(){
string s = "hello world";
decltype(s.size()) index = 0;
while(index <= s.size()){
s[index] = 'X';
index++;
}
cout << s << endl;
return 0;
}
for 循环
int main(){
string s = "hello world";
for(decltype(s.size()) index = 0; index <= s.size(); ++index)
s[index] = 'X';
cout << s << endl;
return 0;
}
for循环,方便
string s;
cout << s[0] << endl;
不合法,因为不能用下标访问空字符串
int main(){
string s = "hello !!! world", new_S;
for(decltype(s.size()) index = 0; index <= s.size(); ++index){
if(!ispunct(s[index])){
new_S += s[index];
}
}
cout << new_S << endl;
return 0;
}
const string s = "Keep out!";
for(auto &c : s){ /* ... */ }
是否合法要看for语句中的内容,c的类型是string对象中字符的引用,
如果for语句中给她赋值就非法,如果不赋值合法
。
vector<vector<int>> ivec; // 老版不正确在C++11当中合法
vector<string> svec = ivec; // 不正确,类型不一样
vector<string> svec(10, "null"); // 正确
vector<int> v1; // 空vector,v1执行默认初始化
vector<int> v2(10); // 10个值为0的元素
vector<int> v3(10, 42); // 10个值为42的元素
vector<int> v4{ 10 }; // 一个值为10的元素
vector<int> v5{ 10, 42 }; // 2个元素,一个值为10,另一个为42
vector<string> v6{ 10 }; // 10个元素,默认是空字符串
vector<string> v7{ 10, "hi" }; // 10个值为“hi”的字符串
int main(){
int i;
vector<int> v;
while(cin >> i){
v.push_back(i);
}
}
int main(){
string i;
vector<string> v;
while(cin >> i){
v.push_back(i);
}
}
int main(){
// vector v1;
// vector v2(10);
// vector v3(10, 42);
// vector v4{ 10 };
// vector v5{ 10, 42 };
// vector v6{ 10 };
vector<string> v7{ 10, "hi" };
cout << "size: " << v7.size() << endl;
// cout << v2.size() << endl;
cout << "value: ";
// for(decltype(v7.size()) idx = 0; idx != v7.size(); ++idx){
// cout << v7[idx] << " ";
// }
for(auto i : v7){
cout << i << " ";
}
cout << endl;
}
int main(){
string word;
vector<string> text;
while(cin >> word){
text.push_back(word);
}
for(auto &c : text){
for(auto &i : c){
i = toupper(i);
}
}
// for(decltype(text.size()) idx = 0; idx != text.size(); ++idx){
// text[idx] = toupper(text[idx]);
// }
for(auto c : text){
cout << c << endl;
}
}
vector<int> ivec;
ivec[0] = 42;
vector<int> v1(10, 42);
vector<int> v2{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
vector<int> v3;
for (int i = 0; i < 10; ++i)
v3.push_back(42);
显然,第一种好
int main()
{
int i;
vector<int> i_vec;
while (cin >> i)
{
i_vec.push_back(i);
}
for (int i = 0; i < i_vec.size() - 1; ++i)
{
cout << i_vec[i] << " + " << i_vec[i + 1] << i_vec[i]+i_vec[i + 1] << endl;
}
//---------------------------------
cout << "---------------------------------" << endl;
int m = 0;
auto n = i_vec.size() - 1;
while (m < n)
{
cout << i_vec[m] + i_vec[n] << endl;
++m;
--n;
}
return 0;
}
自己也写了,但是这个答案是参照的,对于第二问,我总是忘记可以设置两个类似于指针作用的变量,要记住!!!
int main(){
// vector v1;
// vector v2(10);
// vector v3(10, 42);
// vector v4{ 10 };
// vector v5{ 10, 42 };
// vector v6{ 10 };
vector<string> v7{ 10, "hi" };
// int
for(auto it = v7.cbegin(); it != v7.cend(); ++it){
cout << *it << " ";
}
//string
for(auto it = v7.cbegin(); it != v7.cend() && !it->empty(); ++it){
cout << *it << " ";
}
return 0;
}
int main(){
vector<string> text = {10, "hi"};
auto it1 = text.cbegin();
cout << typeid(it1).name() << endl;
cout << typeid(*it1).name();
for(auto it = text.begin(); it != text.end() && !it->empty(); ++it){
for(auto &c : *it){
if(isalpha(c))c = toupper(c);
}
}
for(auto it = text.begin(); it != text.end() && !it->empty(); ++it){
cout << *it << " ";
}
cout << endl;
return 0;
}
int main(){
vector<int> v1(10, 2);
for(auto it = v1.begin(); it != v1.end(); ++it){
*it = *it * 2;
}
for(auto it = v1.begin(); it != v1.end(); ++it){
cout << *it << " ";
}
cout << endl;
for(auto &c : v1){
c = c * 2;
cout << c << " ";
}
cout << endl;
return 0;
}
int main(){
int i = 0;
vector<int> i_vec;
cout << "请输入:" << endl;
while(cin >> i){
i_vec.push_back(i);
}
auto beg = i_vec.begin(), end = i_vec.end()-1;//end指的是尾端的下一个元素
// while(beg != end){
// cout << *beg + *(beg+1) << " ";
// ++beg;
// }
// cout << endl;
while(beg < end){
cout << *beg + *end << " ";
++beg;
--end;
}
return 0;
}
int main(){
vector<unsigned> scores(11,0);
unsigned grade;
auto beg = scores.begin();
while(cin >> grade){
if(grade <= 100){
++(*(beg+grade/10));
}
}
}
因为vector可能存很多东西,beg + end可能超过了边界,会报错。
unsigned buf_size = 1024;
(a) int ia[buf_size];
(b) int ia[4 * 7 - 14];
(c) int ia[txt_size()];
(d) char st[11] = "fundamental";
string sa[10];//空字符串
int ia[10];//0
int main() {
string sa2[10];//空字符串
int ia2[10];//未定义
}
定长,增加元素不灵活,不能够拷贝和赋值。
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix)
ia[ix] = ix;
int main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
return 0;
}
int main(){
int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int arr2[10];
for(size_t idx=0; idx != 10; ++idx){
arr2[idx] = arr[idx];
}
for(auto idx : arr2){
cout << idx << " ";
}
cout << endl;
vector<int> v1(10, 2);
vector<int> v2;
v2 = v1;
for(auto c : v2){
cout << c << " ";
}
cout << endl;
return 0;
}
数组的值将会显示未定义。
p1 += p2 - p1;
将p1指针移动到p2的位置,任何时候都合法
int main(){
int a[5] = {1,2,3,4,5};
for(int * p = a; p != end(a); ++p){
*p = 0;
}
for(int i = 0; i < 5; ++i){
cout << a[i] << " ";
}
cout << endl;
return 0;
}
待更新
const char ca[] = { 'h', 'e', 'l', 'l', 'o' };
const char *cp = ca;
while (*cp) {
cout << *cp << endl;
++cp;
}
打印字符数组ca中的元素,但是因为该数组没有放置空字符串,循环要遇到空字符串才停。
指针存储的是对象的地址,两个指针相减可以表示距离(同一数组中),指针加上整数表示向前或者向后移动整数个位置,相加,如果是两个无关地址,地址相加???没有逻辑上的意义。
int main(){
string s1 = "Hello!";
string s2 = "hello!";
if(s1 == s2){
cout << "The two strings are equal" << endl;
}else if(s1 > s2){
cout << "s1" << endl;
}else{
cout << "s2" << endl;
}
const char ca[] = {"Hello"};
const char ca1[] = {"hello"};
if(strcmp(ca, ca1) == 0){
cout << "The two strings are equal" << endl;
}else if(strcmp(ca, ca1) > 0){
cout << "ca" << endl;
}else{
cout << "ca1" << endl;
}
}
const char ca[] = {"Hello"};
const char ca1[] = {"hello"};
int main(){
char total_ca[12]={};
strcpy(total_ca, ca);//不能写成total_ca = strcpy(total_ca, ca);因为数组名是一个不能被更改的对象。
strcat(total_ca, " ");
strcat(total_ca, ca1);
cout << total_ca << endl;
return 0;
}
int main(){
int arr[5] = {1,2,3,4,5};
vector<int> i_vec(begin(arr), end(arr));
for(auto c : i_vec){
cout << c << " ";
}
return 0;
}
int main(){
vector<int> i_vec;
for(int i = 0; i < 5; ++i){
// i_vec[i] = i; 错误写法
i_vec.push_back(i);
}
for(auto c : i_vec){
cout << c << " ";
}
cout << endl;
cout << "==========" << endl;
int arr[5];
for(int i = 0; i != i_vec.size(); ++i){
arr[i] = i_vec[i];
}
for(auto c : arr){
cout << c << " ";
}
return 0;
}
int main(){
int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
//版本1
cout << "版本1" << endl;
// for(int *i : ia){
// cout << typeid( i).name() << endl;
// for(int *j : begin(*i)){
// cout << *j << " ";
// }
// cout << endl;
// }
for(const int (&i)[4] : ia){
for(int j : i){
cout << j << " ";
}
cout << endl;
}
cout << "版本2" << endl;
// 版本2
for(size_t i = 0; i != 3; ++i){
for(size_t j = 0; j != 4; ++j){
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "版本3" << endl;
// 版本3
for(int (*p)[4] = begin(ia); p != end(ia); ++p){
for(int *q = begin(*p); q != end(*p); ++q){
cout << *q <<" ";
}
cout << endl;
}
return 0;
}
//使用类型别名
int main(){
int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
//版本1
cout << "版本1" << endl;
// for(int *i : ia){
// cout << typeid( i).name() << endl;
// for(int *j : begin(*i)){
// cout << *j << " ";
// }
// cout << endl;
// }
using int_array = int [4];
for(const int_array &i : ia){
for(int j : i){
cout << j << " ";
}
cout << endl;
}
cout << "版本2" << endl;
// 版本2
for(size_t i = 0; i != 3; ++i){
for(size_t j = 0; j != 4; ++j){
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "版本3" << endl;
// 版本3
for(int_array *p = begin(ia); p != end(ia); ++p){
for(int *q = begin(*p); q != end(*p); ++q){
cout << *q <<" ";
}
cout << endl;
}
return 0;
}
//3.45 用auto关键字
int main(){
int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
//版本1
cout << "版本1" << endl;
// for(int *i : ia){
// cout << typeid( i).name() << endl;
// for(int *j : begin(*i)){
// cout << *j << " ";
// }
// cout << endl;
// }
// using int_array = int [4];
for(auto & i : ia){
for(auto j : i){
cout << j << " ";
}
cout << endl;
}
cout << "版本2" << endl;
// 版本2
for(auto i = 0; i != 3; ++i){
for(auto j = 0; j != 4; ++j){
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "版本3" << endl;
// 版本3
for(auto p = begin(ia); p != end(ia); ++p){
for(auto q = begin(*p); q != end(*p); ++q){
cout << *q <<" ";
}
cout << endl;
}
return 0;
}