cpp 学习笔记
1、C++中模仿gets是 getline(cin, string object)
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; struct CanyBar {
string info; // 存储匹萨的品牌
double weight; // 披萨的重量
double diameter;; // 披萨的直径
void showInfomation();
} piSa;
void CanyBar::showInfomation() {
cout << "品牌" << " " << info << endl;
cout << "重量" << " " << weight << endl;
cout << "直径" << " " << diameter << endl;
}
void work() {
cout << "请输入披萨的品牌 :";
// getline()
getline(cin, piSa.info);
// cin >> piSa.info;
cout << "请输入披萨的直径 :";
cin >> piSa.diameter;
cout << "请输入披萨的重量 :";
cin >> piSa.weight;
piSa.showInfomation();
} int main() {
//#ifdef local
// freopen("data.txt", "r", stdin);
//// freopen("data.txt", "w", stdout);
//#endif
work();
return ;
}
2、C++的array好像没什么用啊,
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; void work() {
array<double, > a;
double sum = ;
cout << "请输入三次40米跑成绩" << endl;
for (int i = ; i < ; ++i) {
cin >> a[i];
sum += a[i];
}
cout << sum / << endl;
} int main() {
work();
return ;
}
3、input file stream ----> ifstream input output stream ---> iostream
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
void work() {
ifstream input("data.txt");
char ch;
int ans = ;
if (input.is_open()) {
string s;
while (!input.eof()) {
input.get(ch);
ans++;
}
input.close();
cout << ans - << endl; //会读取'\n',
//回车 + 换行 两个字符
}
} int main() {
//#ifdef local
// freopen("data.txt", "r", stdin);
//// freopen("data.txt", "w", stdout);
//#endif
work();
return ;
}
4、要返回指针,不能用局部变量
// arrfun3.cpp -- array functions and const
#include <iostream>
const int Max = ; // function prototypes
int* fill_array(double ar[], int *begin, int *end);
void show_array(const double ar[], int *end); // don't change data
void revalue(double r, double ar[], int *end); int main() { //要返回指针,不能用局部变量
using namespace std;
double properties[Max];
int begin = , end = ;
int *size = fill_array(properties, &begin, &end);
show_array(properties, size);
if (*size > ) {
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) { // bad input
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return ;
} int * fill_array(double ar[], int *begin, int *end) {
using namespace std;
double temp;
static int i;
for (i = *begin; i < *end; i++) {
cout << "Enter value #" << (i + ) << ": ";
cin >> temp;
if (!cin) { // bad input
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
} else if (temp < ) // signal to terminate
break;
ar[i] = temp;
}
return &i; //这个会消失,所以用不了,或者static
} // the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], int *end) {
using namespace std;
for (int i = ; i < (*end); i++) {
cout << "Property #" << (i + ) << ": $";
cout << ar[i] << endl;
}
} // multiplies each element of ar[] by r
void revalue(double r, double ar[], int *end) {
// cout << (*end) << endl;
for (int i = ; i < (*end); i++)
ar[i] *= r;
}
5、template <typename T>
模板不会生成函数的定义,也就是不会生成
void work() {
printf("fff"); // 这样的具体的函数
}
但是它却会生成函数定义的方案
template <> void swap(int &, int &); // 显示具体化
template <> void swap<int>(int &, int &); // 显示具体化
template void swap(int &, int &); // 显示实例化,少了一个在template后面的<>
title1:编写一个模板函数,接受一个类型为T的数组,和一个n表示数组长度
寻找数组中最大值。
template <class T>
T maxn(T *a, int len) {
T mx = a[];
for (int i = ; i < len; ++i) mx = max(mx, a[i]);
return mx;
}
需要一个实例化,接受一个char *str[]的数组,数组中每一个指针指向一个字符串,然后求出最长的那个字符串。
关键是怎么传参,先来探讨下二维数组要怎么传参
int f[][N];
void fun(const int f[][N]) {
f[][] = ; // 编译失败
printf("%d\n", f[][]);
}
①、第二维的大小必须指出,因为,f[1][0]的寻址方式是,*((*(f + 1)) + 0),因为二维数组中,在内存里面也是连续存放的,需要告诉系统,每次 + 1需要跳多远,就是跳N格。
②、要知道其本质,二维数组的名字,就是一个指向长度为N的一维数组的指针。这样是得f + 1每次跳N格就好。
所以,是这样写
void fun(const int (*f)[N]) {
printf("%d\n", f[][]);
}
③、如果你是用int **p = new的,则需要fun(int **p),也就是什么样的类型,什么样的形参
二维数组传参
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; template <class T>
T maxn(T *a, int len) {
T mx = a[];
for (int i = ; i < len; ++i) mx = max(mx, a[i]);
return mx;
}
template <> char * maxn(char *str[], int len) {
char *id = str[];
int mx = strlen(str[]);
for (int i = ; i < len; ++i) {
if (mx < strlen(str[i])) {
mx = strlen(str[i]);
id = str[i];
}
}
return id;
}
int a[];
char *str[];
void work() {
for (int i = ; i < ; ++i) str[i] = new char[];
strcpy(str[], "");
strcpy(str[], "");
strcpy(str[], "");
strcpy(str[], "");
cout << maxn(str, ) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
char **str,不是二维数组,他是二级指针,和char *str[]一样
所以是这样
char **str;
str = new char *[4]; //先分配
for (int i = 0; i < 4; ++i) str[i] = new char[22];
而二维数组 char str[][],对应的是char (*str)[]
int (*p)[22] = new int[33][22]; // 可以
int (*p)[c] = new int[22][c]; // 不可以。形参不可以。
const int c = 22;
int (*p)[c] = new int[22][c]; //可以
#include <bits/stdc++.h>
#include <windows.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
struct Node {
int w, v; // w物品重量, v是物品体积
Node(int _w, int _v) : w(_w), v(_v) {}
};
int calc_dp(const vector<Node> vc, const int c) { //c是背包容量
int **dp = new int *[vc.size()];
for (int i = ; i < vc.size(); ++i) dp[i] = new int[c + ];
// int (*dp)[c + 1] = new int[vc.size()][c + 1]; 这样不行
for (int i = ; i < vc.size(); ++i) dp[i][] = ;
for (int i = ; i <= c; ++i) {
if (i >= vc[].w) dp[][i] = vc[].v;
else dp[][i] = ;
} bool *sel = new bool[vc.size()];
for (int i = ; i < vc.size(); ++i) sel[i] = false; for (int i = ; i < vc.size(); ++i) {
for (int j = ; j <= c; ++j) {
if (j >= vc[i].w) dp[i][j] = max(dp[i - ][j], dp[i - ][j - vc[i].w] + vc[i].v);
else dp[i][j] = dp[i - ][j];
}
}
int tc = c;
for (int i = vc.size() - ; i >= ; --i) {
if (dp[i][tc] != dp[i - ][tc]) {
sel[i] = true;
tc -= vc[i].w;
}
}
if (tc) sel[] = true;
for (int i = ; i < vc.size(); ++i) printf("%d ", sel[i]);
printf("\n");
return dp[vc.size() - ][c];
} int mxState, mxValue; void calc_baoli(const vector<Node> vc, int c, int now, int state, int ans) { // calc_baoli(vc, c, 0)
if (now == vc.size()) {
if (mxValue < ans) {
mxValue = ans;
mxState = state;
// printf("%d %d\n", ans, state);
}
return ;
}
calc_baoli(vc, c, now + , state, ans);
if (c >= vc[now].w) {
calc_baoli(vc, c - vc[now].w, now + , state | ( << now), ans + vc[now].v);
}
} int calc_greedy(const vector<Node> vc, int c) { // calc_greedy(vc, c)
vector< pair<double, int> > b;
b.clear();
for (int i = ; i < vc.size(); ++i) b.push_back(make_pair(1.0 * vc[i].v / vc[i].w, i));
sort(b.begin(), b.end(), greater< pair<double, int> >());
bool *sel = new bool[vc.size()];
for (int i = ; i < vc.size(); ++i) sel[i] = false;
int ans = ;
for (int i = ; i < b.size(); ++i) {
if (c >= vc[b[i].second].w) {
c -= vc[b[i].second].w;
ans += vc[b[i].second].v;
sel[b[i].second] = true;
}
}
for (int i = ; i < vc.size(); ++i) printf("%d ", sel[i]);
printf("\n");
return ans;
}
void work_dp(vector<Node> vc, int c) {
long long be = clock();
printf("动态规划的答案: %d 时间: ", calc_dp(vc, c));
long long t = clock() - be;
printf("%lld\n\n", t);
} void work_greedy(vector<Node> vc, int c) {
long long be = clock();
printf("贪心的答案: %d 时间: ", calc_greedy(vc, c));
long long t = clock() - be;
printf("%lld\n\n", t);
} void work_baoli(vector<Node> vc, int c) {
long long be = clock();
mxState = , mxValue = -;
calc_baoli(vc, c, , , );
for (int i = ; i < vc.size(); ++i) {
if (mxState & ( << i)) {
printf("1 ");
} else printf("0 ");
}
printf("\n");
printf("暴力的答案: %d 时间: ", mxValue);
long long t = clock() - be;
printf("%lld\n\n", t);
} void work() {
vector<Node> vc;
vc.clear();
int n, c;
srand(time(NULL));
printf("---------请输入背包容量-------------------\n");
scanf("%d", &c);
printf("---------请输入物品个数-------------------\n");
scanf("%d", &n);
printf("---------请输入%d个物品-------------------\n", n);
for (int i = ; i < n; ++i) {
int w, v;
w = rand() % + ;
v = rand() % + ;
printf("%d %d\n",w,v);
vc.push_back(Node(w, v));
}
work_dp(vc, c);
work_greedy(vc, c);
work_baoli(vc, c);
} int main() {
//#ifdef local
// freopen("data.txt", "r", stdin);
//// freopen("data.txt", "w", stdout);
//#endif
work();
return ;
}
函数调用时候选择的优先级
1、完全匹配,但常规函数优于模板
2、提升转换,char和short自动去int, float自动去double
3、标准转换,int 转为 char, long 转为 double
4、用户定义的转换
6、在函数中的形参放入const,只是一个修饰符,表明这个形参是不可以变化的,不是定义一个不可变化的常量一样。
所以当我们传入参数进去的时候,不用以为const也是可变的。这里的const和const int a = 1, //a不可变,是不同的。
和重载运算符那个一样的。
再比如strcmp(const char * str, const char * sub) // 这个称谓常量指针,确保这个指针不能修改那块内存的东西,但是却可以改变这个指针指向的内存位置。 “传入来的指针就算变了指向,和一开始的也是没关系的
相当于我把家里的钥匙复制一份给你,你把他扔了,和我无关”
还有一个就是指针常量, int * const p = &a,说明这个指针的指向不能再改变。
1、模板函数、或者模板类需要在.h文件下给出函数的定义,因为在其他一个cpp里面,只include了它的.h文件,所以如果你只是声明了的话,在cpp里面就无法找到它的定义,也就是无法按照模板生成一个具体的函数了,这样做编译的时候没问题,连接程序的时候会失败。
解决方案
1、include file2.cpp,这个操作很骚
2、在file2.cpp中来一个显式实例化,但是这就违背了模板的作用,难道你为每一个都显式实例化吗。也好,分为int和double两种而已。
3、在.h中给出定义(最优), STL也是这么用
https://zhidao.baidu.com/question/1946414924223767268.html
-------------------------2017年12月22日 21:01:30--------------------------------------------------
输入的东西会放去缓冲区,比如输入两个string1, string2可以cin >> s1, cout << s1, cin >> s2,也是可以得。
就是asb sba
这样可以两个串都同时得到。
用cin.sync(); 可以清除缓冲区,这样第二个字符串就输入不了了。或者fflush(stdin)
http://blog.csdn.net/livecoldsun/article/details/25489429
注意用cin.get(str, 6, 结束符),只能最多读取5个字符,因为有一个是回车,而且他不接受回车,让它留在缓冲区、
--------------------------------------2018-1-4 14:19:13-------------------------------------------------------------
如果一个函数是返回一个引用,这个时候就要注意了,因为返回引用的话,calc(val) = 2是可以直接赋值的,这样在if的时候可能会出错
虽然我觉得if那里很难写错,可能以后熬夜写代码会写错。
所以,应该考虑下是否要放回const的引用。
int & calc(int val) {
int ans = ;
while (val / > ) {
ans += val % ;
val /= ;
}
ans += val;
return ans;
}
没返回const,危险
重载运算符的时候,我刚才在重载 = 这个运算符,这个运算符应该是在里面搞事。
先说说重载会怎样。
比如重载了+号,那么a + c就会退化成a.operator+(c),调用这样的一个函数。
然后a = a + c,返回一个一模一样类型的值给a,所以a的值有改变。。。。
我刚才也一样,重载了 = ,也是返回一个一模一样的类型值,以为a = c也会返回一个一模一样的值给a,实则不然。
这其实相当于a.operator=(c),然后就没了,他不是a = a.operator=(c),就算是这样,也是徒劳,递归一次就知道这是错的。
所以重载 = 应该是在里面搞事,在里面就改了a的值了。而重载其他的 + 之类的,直接重载即可。
再一次解释,因为它一般会这样用,a = a + c这样退化成a = a.operator+(c);,这样返回一个一模一样的类型,赋值给a,是可以得。
2018-09-10 10:38:45
1、类的初始化成员列表用处是:修改类内部的const int a;成员,这个必须在初始化成员列表里面弄。
2、派生类使用不了基类的protected成员,只能在派生类里面的方法里使用。比如在派生类B中定义print(B &test),接受一个B类型的对象,在里面调用就可以
cpp 学习笔记的更多相关文章
- cpp学习笔记 1一个简单的小程序以及一些的知识点
今天买的cpp到了从今天開始又一次学习cpp如今发现学校发的书真的不怎莫样. <em>#include<stdio.h>//预处理命令 int main()/*第一个被调用的函 ...
- OpenCV学习笔记(一)——OpenCV3.1.0+VS2015开发环境配置
摘要: 由于最近AR(增强现实)这个概念非常火爆,各种基于AR的应用及游戏逐渐面向大众,而在AR中最重要的两个技术就是跟踪识别和增强渲染,其中跟踪识别是通过OpenCV这个开源的计算机视觉库来实现的, ...
- Caffe学习笔记2--Ubuntu 14.04 64bit 安装Caffe(GPU版本)
0.检查配置 1. VMWare上运行的Ubuntu,并不能支持真实的GPU(除了特定版本的VMWare和特定的GPU,要求条件严格,所以我在VMWare上搭建好了Caffe环境后,又重新在Windo ...
- [转]Windows平台下Makefile学习笔记
Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...
- C++学习笔记(3)
本学习笔记是C++ primer plus(第六版)学习笔记.是C++学习笔记(2)的后续.复习C++基础知识的可以瞄瞄. 转载请注明出处http://www.cnblogs.com/zrtqsk/p ...
- 烤鸭的Source Insight学习笔记
如果你觉得这网页排版不好看,可以去下载我上传的word版:<烤鸭的Source Insight学习笔记.doc> http://download.csdn.NET/detail/benka ...
- 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1
本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...
- DuiLib学习笔记(二) 扩展CScrollbar属性
DuiLib学习笔记(二) 扩展CScrollbar属性 Duilib的滚动条滑块默认最小值为滚动条的高度(HScrollbar)或者宽度(VScrollbar).并且这个值默认为16.当采用系统样式 ...
- Makefile的学习笔记
Makefile的学习笔记 标签: makefilewildcard扩展includeshellfile 2012-01-03 00:07 9586人阅读 评论(2) 收藏 举报 分类: Linux ...
随机推荐
- Spring-访问静态资源文件的方法
转自: http://blog.163.com/zhangmihuo_2007/blog/static/27011075201453044959574?suggestedreading 如果你的Di ...
- 禁用GridView控件前5行记录
禁用GridView控件前5行记录. 应该在GridView控件写OnRowDataBound事件: 如果你只想禁用删除铵钮的话: 网页运行效果: 如果你想把整行禁用的话,可以这样写: 运行效果: 禁 ...
- 混合APP开发-hybrid 升级流程
本文来自网易云社区 作者:王贝 目前大多数APP已经应用hybrid进混合开发,这不,我们的gacha APP这个版本已经开始使用hybrid来开发了,hybrid的优势这里就不多说了,这里主要讲一下 ...
- Mybatis+maven自动构建dao、mapper、model
1.Pom.xml文件配置: 代码: <plugins> <!-- Mybatis generator代码生成插件 配置 --> <plugin> <grou ...
- Windows10远程报错:由于CredSSP加密Oracle修正(ps:Win10家庭版)
Windows10远程桌面连接 报错信息 : 网上找到方法 但是奈何是 "Win10家庭版" 不能使用这个办法,具体操作可以看最后的引用链接 !!!! 策略路径:“计算机配置”-& ...
- pychram编写代码鼠标变粗处理
现象:如下图 解决: 键盘 点击 insert 按钮即可解决
- c++语言的组合类的使用,用组合类的方法计算两点间距离。
组合类的使用主要涉及到类的构造函数,类的复制构造函数. #include <iostream> #include<cmath> class Point{ public: Poi ...
- LAMP之Apache
Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.快速.可靠并且可通过简单的API扩充,将 ...
- anularjs前端分页
var dataList = []; $scope.page = { page:1, pageSize:15, numPerPageOpt:[15,30,50,100], totalNum:0, to ...
- Sublime Text3安装、配置及常用插件(陆续补全中~)
一.安装Sublime Text3 网址:http://www.sublimetext.com/3 注册码:(sublime Text3汉化和激活注册码) ----- BEGIN LICENSE -- ...