c++的相关知识点

将使用两个代码说明c++的相关知识点:

第一个:输入数据的每行包括若干个(至少一个)以空格隔开的整数,输出每行所有的整数和。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<sstream>
#include <map> 
#include <stack> 
#include <set>
#include<queue>
#include<algorithm>
using namespace std;
//  #include<bits/stdc++.h>
//  using namespace std;
int main()
{
	string line;
	while(getline(cin,line)){
		int sum=0,x;
		stringstream  ss(line);
		while(ss >> x)
			sum+=x;
		
		cout << sum << "\n"; 
	}
	
	return 0;
}

1.头文件

    通过这个代码可见c++常见的头文件的类型:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<sstream>
    #include <map> 
    #include <stack> 
    #include <set>
    #include<queue>
    #include<algorithm>
    using namespace std;

    .........等等。

    但   #include<bits/stdc++.h>
          using namespace std; 包含上述所有的头文件。但有的oj上不支持这种写法。

2.第二个代码:使用c++进行结构体的相关运算:

#include<iostream>
using namespace std;
const int maxn=1e5+10;
struct point{
	int x,y;
	point(int x=0,int y=0) : x(x),y(y) {}
}f[maxn];
point operator + (const point& A,const point& B){
	return point(A.x+B.x, A.y+B.y);
} 
ostream& operator << (ostream &out,const point& p){
	out <<"("<<p.x<<","<<p.y <<")";
	return out;
}
int main()
{
	point a,b(1,2);
	a.x=3;
	
	cout << a+b << "\n";
		
	return 0;
}

 

构造函数:

       1. 结构体point中定义了一个函数,函数名也叫point.但是没有返回值。这样的函数称为构造函数。

          构造函数是在声明变量时调用的,例如,声明Point a, b(1,2)时, 分别调用了Point()和Point(1,2)。

          注意这个构造函数的两个参数后面都有“=0”字样,其中0为默认值。也就是说,如果没有指明这两

        个参数的值,就按0处理,因此Point()相当于Point(0,0)。
      2.“:x(x),y(y)”则是一个简单的写法,表示“把成员变量x初始化为参数x,成员变量y初始化为参数y”。

         也可以写成:Point(int x=0,int y=0) { this->x = x; this->y = y }
         这里的“this" 是指向当前对象的指针。this->x 的意思是“当前对象的成员变量x”,即(*this).x。

   3. 提示: C++中的结构体可以有一个或多个构造函数,在声明变量时调用。
       提示: C+中的函数(不只是构造函数)参数可以拥有默认值。
       提示:在C++结构体的成员函数中,this 是指向当前对象的指针。
   4  .重载,接下来为这个结构体定义了“加法”,并且在实现中用到构造函数。这样,就可以用
        a+b的形式计算两个结构体a和b的“和”了。
        最后定义这个结构体的流的输出方式,然后就可以用cout<<p来输出一个point结构体p了
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值