战士 发表于 2008-7-13 15:13

面试题大家试试

1、用一个类实现两个数组相加,例如数组a=b+c;就是响应位置的数相加等于响应的另外一个数组的元素。
2、读取一个文件中的数据,并且排序,求出第500个小的数。
3、替换字符串中的\t为4个*。
4、char占内存几个字节?
5、请解释线程和进程的区别,别说明什么时候用多进程效率高,什么时候单线程效率高。
6、请解释虚基类,并说明怎么应用。
剩下的输出结果的题太难打出来。有时间再给大家发了。

linren 发表于 2008-7-13 21:46

2、读取一个文件中的数据,并且排序,求出第500个小的数。

[bo]程序:[/bo]
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main ()
{
        vector<int> a;
        int at;

        ifstream in("data.txt",ios::binary);
        while(in.read((char *)&at,sizeof at))
        {
                a.push_back(at);
        }
        in.close();

        sort(a.begin(),a.end());
        cout<<a[500-1]<<endl;

        return 0;
}

linren 发表于 2008-7-13 22:49

3、替换字符串中的\t为4个*。

[bo]程序:[/bo]
#include <iostream>
#include <string>
using namespace std;

int main ()
{
        string s;
        s+="abcd";
        s+='\t';
        s+="12345";
        s+='\t';
        s+="@$$%^$%#";
        cout<<s<<endl;

        int l=s.find('\t');
        while(l!=string::npos)
        {
                s.replace(l,1,"****");
                l=s.find('\t',l+4);
        }

        cout<<s<<endl;
        return 0;
}

linren 发表于 2008-7-13 23:25

1、用一个类实现两个数组相加,例如数组a=b+c;就是响应位置的数相加等于响应的另外一个数组的元素。

[bo]程序:[/bo]
#include <iostream>
using namespace std;

class A
{
public:
        void add(int c[],const int a[],const int b[],const int n);
};

void A::add(int c[],const int a[],const int b[],const int n)
{
        for(int i=0;i<n;i++)
                c[i]=a[i]+b[i];
}

int main ()
{
        int a[3]={1,2,3};
        int b[3]={2,1,5};
        int c[3];

        A op;
        op.add(c,a,b,3);

        cout<<c[0]<<", "<<c[1]<<", "<<c[2]<<endl;

        return 0;
}

linren 发表于 2008-7-13 23:32

1、用一个类实现两个数组相加,例如数组a=b+c;就是响应位置的数相加等于响应的另外一个数组的元素。

[bo]程序(使用模版):[/bo]
#include <iostream>
using namespace std;

template<class T>
class A
{
public:
        void add(T c[],const T a[],const T b[],const int n);
};

template<class T>
void A<T>::add(T c[],const T a[],const T b[],const int n)
{
        for(int i=0;i<n;i++)
                c[i]=a[i]+b[i];
}

int main ()
{
        int a[3]={1,2,3};
        int b[3]={2,1,5};
        int c[3];
        A<int> op;
        op.add(c,a,b,3);
        cout<<c[0]<<", "<<c[1]<<", "<<c[2]<<endl;

       
        double a1[2]={1.25,5.47};
        double b1[2]={-3.47,6.7};
        double c1[2];
        A<double> op1;
        op1.add(c1,a1,b1,2);
        cout<<c1[0]<<", "<<c1[1]<<endl;

        return 0;
}

alexboy 发表于 2008-7-14 08:12

[tk01] 5,6都比较囧

lindayanglong 发表于 2008-7-16 10:01

不明白string::npos请解释一下

不明白string::npos得到的是什么东西 很想知道 请解释一下 谢谢

linren 发表于 2008-7-16 21:45

[quote][bo][un]lindayanglong[/un] 在 2008-7-16 10:01 的发言:[/bo]

不明白string::npos得到的是什么东西 很想知道 请解释一下 谢谢 [/quote]
find失败后的返回值……

页: [1]

编程论坛