精华内容
下载资源
问答
  • Friend

    2017-01-24 08:16:27
    Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; (2) if a and b are friend numbers, so is ab+a+b; (3) only the numbers defined in (1) and (2) are friend number....
  • friend

    2019-10-06 22:39:18
    friend关键字:友元 友元函数,友元类 作用:友元是为了突破原有的private,protected权限 1. 在某个类中声明友元,表示被声明的友元函数或友元类对该类有任意的直接访问权 2. 友元不受声明处的访问权限控制符...

    friend关键字:友元

    友元函数,友元类

    作用:友元是为了突破原有的private,protected权限

    1. 在某个类中声明友元,表示被声明的友元函数或友元类对该类有任意的直接访问权

    2. 友元不受声明处的访问权限控制符影响,因为friend朋友都已经得到直接访问权了,还需要考虑这个声明符吗?

    3. 友元本质上不属于被声明处的那个类,要是是裸函数,要么是类,要么是其他类的成员函数

    4. 友元friend朋友,不具备继承特性。父亲的朋友不一定是孩子的朋友,各自有各自的朋友比较好

    5. 那么在派生类中的友元函数,通过派生类对象可以访问基类的protected成员,但通过基类对象访问基类的protected

        成员就不行。为什么?这个友元函数是派生类的朋友,又不是基类的朋友   ^__^

    转载于:https://www.cnblogs.com/lizckevin/articles/7097277.html

    展开全文
  • Prime Friend

    2020-03-14 22:00:04
    Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prime Friend. We call a nonnegative integer A is the integer B’s Prime Friend when the sum of A and B ...
  • Friend Number

    2017-09-09 03:01:22
    Friend numbers are those who are composed of the same digits, for example 3123 and 11233233 are friend numbers, but 1233432 and 123331 are not friend numbers because in the second number the 4 is ...
  • <p>I thought I needed a friend provider to implement add and delete friends. So first, I made a friend provider using the "immer" library presented in the issue, and applied it to the [Search...
  • Friend Circles

    2017-06-23 21:15:53
    Friend Circles

    题目来源LeetCode
    题目描述

    There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

    Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

    Example 1:
    Input:
    [[1,1,0],
    [1,1,0],
    [0,0,1]]
    Output: 2

    Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
    The 2nd student himself is in a friend circle. So return 2.

    Example 2:
    Input:
    [[1,1,0],
    [1,1,1],
    [0,1,1]]
    Output: 1

    Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
    so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

    Note:
    N is in range [1,200].
    M[i][i] = 1 for all students.
    If M[i][j] = 1, then M[j][i] = 1.

    这道题其实是图论问题,求图的连通分量,通过DFS来遍历朋友,代码如下:

    void Findfriend(vector<vector<int>>& M,bool judge[],int m)
         {
             judge[m] = true;
             for (int i = 0; i < M.size();i++)
             {
                 if (judge[i] == false &&  M[m][i]==1)  Findfriend(M,judge, i);
             }
         }
    
    class Solution {
    public:
         int findCircleNum(vector<vector<int>>& M)
         {
             int row = M.size();
             if (row == 0)return 0;         
             bool judge[row+1];
             memset(judge,false,sizeof(judge));
             int cir_num = 0;
             for (int i = 0; i < row;i++)
             {
                 if (judge[i]==false)
                 {
                     Findfriend(M, judge, i);
                     cir_num++;
                 }
             }
             return cir_num;
         }
    };
    展开全文
  • C++ friend函数和friend

    2019-12-22 15:12:03
    类的friend函数(友元函数)在类的作用域之外定义,却具有访问类的非public(以及public)成员的权限。单独的函数或者整个类都可以被声明为另一个类的友元。使用friend函数可以提高程序的性能。 定义友元函数: ...

            类的friend函数(友元函数)在类的作用域之外定义却具有访问类的非public(以及public)成员的权限。单独的函数或者整个类都可以被声明为另一个类的友元。使用friend函数可以提高程序的性能。

    定义友元函数:

           在类定义中函数原型前面加保留字friend,就将函数声明为该类的友元。比如:要将类ClassTwo的所有成员函数声明为类ClassOne的友元,应在ClassOne定义中加入如下的形式的一条声明:

           friend class ClassTwo;

    注意点1:即使friend函数的原型在类定义内出现,友元仍然不是成员函数

    注意点2:private ,public , protected 这些成员访问说明符标志与友元的声明无关,因此友元定义可以放到类的任何地方

    注意点3:养成良好的编程友习惯,在类定义中把所有友元关系声明在最前面的位置,并且不要在其前面添加任何成员访问说明符!

    友元关系是授予的不是索取的!

           对于类B是类A的友元,类A必须显示的声明类B是他的友元,另外,友元关系既不是对称的也不是传递的,即如果类A是类B的友元,类B是类C的友元,不能够推断出类A是类C的友元(有缘关系不是传递的),也不能推出类B是类A的友元(友元关系不是对称的),同时也不能推出类C是类B的友元(友元关系不是对称的)。

    举例说明(使用friend函数修改类的private数据):

          在这个例子中,定义friend函数setX来设置Count类的private数据成员x。请注意,友元声明首先出现在类的定义中,甚至出现在public成员函数之前。再次说明,友元声明可以在类的任何地方。

    #include <iostream>
    using namespace std;
    
    //Count 类定义
    class Count
    {
    	friend void setX(Count &,int);//友元函数setX定义
    public:
    	//构造函数
    	Count()
    		:x(0)//初始化x为0
    	{
    		//空的函数体
    	}
    
    	void print()const
    	{
    		cout<<x<<endl;
    	}
    private:
    	int x;
    };
    
    
    //setX函数可以更改count类的私有成员
    //因为setX被定义为count类的友元函数
    void setX(Count &c, int val)
    {
    	c.x= val;//将val变量的值赋给count类c对象的x数据成员
    }
    
    int main()
    {
    	Count counter;
    
    	cout<<"counter.x 经过实例化后是:";
    	counter.print();
    
    	setX(counter,8);//使用友元函数
    	cout<<"counter.x 使用setX友元函数后的值为:";
    	counter.print();
    
    	system("pause");
    	return 0;
    }

    其运行结果是:

    函数setX是一个c语言风格的独立函数——他不是Count类的成员函数。因此,对于counter对象,当setX被调用的时候,第41行将counter作为参数传递给setX,而不是语句 counter.setX(8) ;   一样的句柄来调用此函数。如果把友元的声明去掉,就会出现错误的信息,函数setX不能修改count类的私有成员。

    一般情况下,我们将上述该程序分割成如下的三个文件:

    1. 一个头文件(比如:Count.h)包含Count类的定义,而在类的定义中又包含了friend函数setX的函数模型。
    2. 一个执行文件(比如:Count.cpp)包含Count类成员函数的定义以及friend函数setX的定义。
    3. 一个主函数(比如:main.cpp)含有main函数。

     

    以上就是有关friend友元的基础知识,希望对大家有帮助。

    展开全文
  • Friend Chains

    2020-04-10 14:43:19
    For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of ... So that a chain of “a friend of a friend” can b...

    For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of “a friend of a friend” can be made to connect any 2 persons and it contains no more than 7 persons.
    For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ’s friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
    Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain’s length is no more than k .
    Input
    There are multiple cases.
    For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group.
    Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.
    Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group.
    Each of the next M lines contains two names which are separated by a space ,and they are friends.
    Input ends with N = 0.
    Output
    For each case, print the minimum value k in one line.
    If the value of k is infinite, then print -1 instead.
    Sample Input
    3
    XXX
    YYY
    ZZZ
    2
    XXX YYY
    YYY ZZZ
    0
    Sample Output
    2

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int MAXN =10000;
    const int INF = 1010;
    int dis[MAXN][MAXN];
    bool used[MAXN];
    vector<int>vec[MAXN];
    queue<int>que;
    void dfs(int i) {
    	memset(used, false, sizeof(used));
    	dis[i][i] = 0;
    	used[i] = true;
    	que.push(i);
    	while (!que.empty()) {
    		int t = que.front();
    		que.pop();
    		int m = vec[t].size();
    		for (int j = 0; j < m; j++) {
    			int v = vec[t][j];
    			if (used[v])continue;
    			dis[i][v] = dis[i][t] + 1;
    			que.push(v);
    			used[v] = true;
    		}
    	}
    }
    map<string, int>mp;
    int main() {
    	string str;
    	string str2;
    	int n, m;
    	ios::sync_with_stdio(false);
    	while (cin>> n&&n) {
    		mp.clear();
    		for (int i = 0; i < n; i++) {
    			cin >> str;
    			mp[str] = i;
    		}
    		for (int i = 0; i < n; i++) {
    			dis[i][i] = 0;
    			for (int j = i + 1; j < n; j++)
    				dis[i][j] = dis[j][i] = INF;
    		}
    		cin >> m;
    		for (int i = 0; i < n; i++)vec[i].clear();
    		while (m--) {
    			cin >> str >> str2;
    			int t1 = mp[str];
    			int t2 = mp[str2];
    			vec[t1].push_back(t2);
    			vec[t2].push_back(t1);
    		}
    		for (int i = 0; i < n; i++)dfs(i);
    		int ans = 0;
    		for (int i = 0; i < n; i++) {
    			for (int j = i+1; j< n;j++) {
    				ans = max(ans, dis[i][j]);
    			}
    		}
    		if (ans == INF)ans = -1;
    		cout <<ans<< endl;
    	}
    
    }
    
    
    展开全文
  • friend友元

    2019-03-06 22:52:22
    friend友元 文章目录friend友元前言相互友元类友元成员函数共同友元 前言 作用:友元类的所有方法 和 友元函数 可以访问原始类的私有成员和保护成员。 友元类的声明:friend class [classname]; 。 友元函数的声明...
  • friend 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。 友元可以是一个函数,该函数被称为...
  • 对于一个没有定义public访问权限的类,能够让其他的类操作它的私有成员往往是有用的。例如你写了一段binary tree的代码,Node是节点类,...C++中的friend关键字其实做这样的事情:在一个类中指明其他的类(或者)函...
  • Friend 的求解

    2019-09-21 22:30:27
    Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; (2) if a and b are friend numbers, so is ab+a+b; (3) only the numbers defined in (1) and (2) are friend ...
  • c++ key word: friend
  • friend关键字

    2018-05-25 14:29:33
    C++学习笔记:friend ostream &amp;operator&lt;&lt;(ostream &amp;stream, const Date&amp; dt);20 friend关键字c++中有个friend关键字,它能让被修饰的对象冲破本class的封装特性,从而能够...
  • Friend(1719)

    2015-04-27 13:52:52
    Friend
  • c++ friend

    2018-08-30 17:52:49
    C++中的friend关键字作用: 在一个类中用friend申明其他的类或函数,申明的类或函数能够直接访问该类中的private和protected成员。 class Pack { private: std::string data ; int size ; public: pack(std:...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 25,360
精华内容 10,144
关键字:

friend