cpplearnnote's People
cpplearnnote's Issues
友元
友元关系是为了在类外部访问private变量而设置的。
有三种友元关系
- 友元函数
- 友元成员函数
- 友元类
友元申明在要被访问的类中使用friend即可。
#include <iostream>
using namespace std;
class B;
class A{
private:
int x;
public:
A();
void display(B &);
};
class C;
class B{
private:
int y;
int z;
public:
B(int y, int z);
friend void A::display(B &);//友元成员函数
friend void display(B &);//友元函数
friend class C;//友元类
};
class C{
private:
int sum;
void calc(B &);
public:
C();
void display(B &);
};
//必须在友元关系的类后进行定义
void display(B &v) {//友元函数
cout<< "友元函数" << v.y << " " << v.z << endl;
}
A::A() {
x = 0;
}
void A::display(B &v) {//友元成员函数
x = v.y + v.z;
cout << "友元成员函数" << x <<endl;
}
B::B(int y, int z) {
this->y = y;
this->z = z;
}
C::C() {
sum = 0;
}
void C::display(B &v) {
calc(v);
cout << "友元类" << sum << " = " << v.y << " + " << v.z <<endl;
}
void C::calc(B &v) {
sum = v.y + v.z;
}
int main() {
A a;
B b(2, 3);
display(b);
a.display(b);
C c;
c.display(b);
}
输出结果为
友元函数2 3
友元成员函数5
友元类5 = 2 + 3
翻转字符串,单词顺序保持不变
#include <iostream>
using namespace std;
class Solution {
public:
void reverseWords(string &s) {
string result;
int pos = 0;
int size = s.size();
for (int i = 0; i < size; i++) {
//遇到空格或者最后一个字符就取到前面的单词并添加空格加到前面
if (s[i] == ' ') {
if (i>pos) {
result = s.substr(pos, i-pos) + " " + result;
}
pos = i + 1;
} else if (i == size - 1) {
result = s.substr(pos, size - pos) + " " + result;
}
}
//由于开始时result是空串所以最后多一个空格,最后去掉。
s = result.substr(0, result.size() - 1);
}
};
int main() {
Solution *s = new Solution();
string str = "zxy love lp";
cout << str << endl;
s->reverseWords(str);
cout << str << endl;
delete s;
}
result:
zxy love lp
lp love zxy
求子数组最大乘积
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProduct(int a[], int n) {
if (n == 0) {
return 0;
}
int maxHerePre = a[0];
int minHerePre = a[0];
int maxSoFar = a[0];
int maxHere, minHere;
for (int i=1; i<n; i++) {
//第一个max是取得这里的最大乘积,第二个max是防止|前面max的结果|<1。
maxHere = max(max(maxHerePre*a[i], minHerePre*a[i]), a[i]);
//第一个min是取得这里的最小乘积,第二个max是防止|前面min的结果|<1。
minHere = min(min(maxHerePre*a[i], minHerePre*a[i]), a[i]);
//比较当前位置的最大值与全局最大值
maxSoFar = max(maxHere, maxSoFar);
//将当前最大最小值设为上一次扫描的最大最小值
maxHerePre = maxHere;
minHerePre = minHere;
}
return maxSoFar;
}
};
template <class T>
int getSize(T& a) {
return sizeof(a)/sizeof(*a);
}
int main() {
Solution *s = new Solution();
int a[] = {2, 3, -2, 4};
int max = s->maxProduct(a,getSize(a));
cout << max << endl;
delete s;
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.