程序的结果为______。include"iostream.h" template T total(T*data) { Ts=0; while(程序的结果为______。include"iostream.h"template<typename T>T total(T*data){Ts=0;while(*data){S+=*data++;}return S;}int main(){int x[]={2,4,6,8,0,12,14,16,18};cout<<total(x);retum 0;cout<<end

题目
程序的结果为______。include"iostream.h" template T total(T*data) { Ts=0; while(

程序的结果为______。

include"iostream.h"

template<typename T>

T total(T*data)

Ts=0;

while(*data)

S+=*data++;

return S;

int main()

{int x[]={2,4,6,8,0,12,14,16,18};

cout<<total(x);

retum 0;

cout<<endl;}


相似考题
更多“程序的结果为______。include"iostream.h" template<typename T> T total(T*data) { Ts=0; while( ”相关问题
  • 第1题:

    下列程序的输出结果是【】 includeusing namespace std; template T total

    下列程序的输出结果是【 】

    include<iOStream>

    using namespace std;

    template <typename T>

    T total (T*datA)

    {

    T s=0;

    while(*datA)

    {

    s+=*data++;

    }

    return s;

    }

    int main()

    {

    int x[]={2,4,6,8,0,12,14,16,18};

    cout<<total(x)<<end1;

    return 0;

    }


    正确答案:20
    20 解析:本题考核函数模板的应用。解此题的关键是理解函数total的作用,total的作用是将数组的元素值相加,直到某个元素值为0截至,并返回相加的结果。数组x的第5个元素的值为0,所以total返回的结果为前4个元素值的相加,即20。

  • 第2题:

    有如下程序: include using namespace std; template T total(T * data)

    有如下程序:

    include<iostream>

    using namespace std;

    template<typename T>

    T total(T * data) {

    T s=0;

    While(* data)s+ = *data + +;

    return s;

    }

    int main(){

    int x[]:{2,4,6,8, 10, 12, 14, 16, 18};

    cout<<total(x);


    正确答案:20
    20

  • 第3题:

    有以下程序includevoid ss(char*s,char t){ while(*s){if(*S==t)*s=t-'a'+'A';s++;

    有以下程序 #include<iostream.h> void ss(char*s,char t) { while(*s) { if(*S==t)*s=t-'a'+'A'; s++;} } void main( ) { char strl[100]="abcddfefdbd",c='d': ss(strl,c) ;cout<<strl;} 程序运行后的输出结果是

    A.ABCDDEfEBD

    B.abcDDfefDbD

    C.abcAAfefALbA

    D.Abcddfefdbd


    正确答案:B
    解析:在内存中,字符数据以ASCII码存储,它的存储形式就与整数的存储形式相类似。C++语言使字符型数据和整型数据之间可以通用。也可以对字符数据进行算术运算,此时相当于对它们的ASCII码进行算术运算,在本题中,s++相当于s=s+1,即让s指向数组中的下一个元素。

  • 第4题:

    阅读下列C++程序和程序说明,将应填入(n)处的字句写在对应栏内。

    【说明】设单链表的结点类和链表类的定义如下,链表不带有表头结点。请填空:

    include<iostream.h>

    include<assert.h>

    template<class T>class List;

    template<class T>class ListNOde{

    friend (1);

    private:

    T data;

    ListNode<T> *link;

    public:

    ListNode():link(NULL)()

    ListNOde(const T& item,ListNOde<T>*next=NULL)

    :data(item),link(next){}

    };

    template<class T>class List{

    private:

    ListNode<T>*first;

    void createList(T A[],int n,int i,ListNOde<T>*&p);

    void printList(ListNOde<T>*p);

    public:

    List();

    ~List();

    friend ostream& operator<<(ostream& ost,List<T>&L);

    friend istream& operator>>(istream& ist,List<T>&L);

    };

    template<class T>

    istream& operator>>(istream& ist,List<T>&1){

    int i,n; ist>>n;

    T A[n];

    for(i=0;i<n;i++) (2);

    createList(A,n,0,first);

    }

    template<class T>

    void List<T>::createList(TA[],int n,int i,ListNOde<T>*& p){

    //私有函数:递归调用建立单链表

    if(i==n)p=NULL;

    else{

    p=new ListNode<T>(A[i]);

    assert(p !=NULL);

    createList((3));

    }

    }

    template<class T>

    ostream& operator<<(ostream& ost,List<T>& L){

    (4);

    }

    template<class T>

    void List<T>::printList(ostream& ost,ListNode<T>*p){

    if(p!=NULL){

    ost<<p->data;

    (5);

    }

    }


    正确答案:(1)class ListT> (2)ist>>A[i] (3)Ani+1p->link (4)printList(ostfirst) (5)printList(p->link)
    (1)class ListT> (2)ist>>A[i] (3)A,n,i+1,p->link (4)printList(ost,first) (5)printList(p->link) 解析:(1)class ListT>
    模板类class T>class List必须声明为模板类class T>class ListNode的友元类,这样模板类class T>class List的所有成员函数都可以直接访问模板类class T>class ListNode的私有成员。
    (2)ist>>A[i]
    重载友元函数>>,从输入流对象ist读取具有n个值的数组A[],调用成员函数createList建立链表L。
    (3)A,n,i+1,p->link
    递归成员函数createList,根据给定的具有n个值的数组A[],建立以指针p为头指针的单链表。
    (4)printList(ost,first)
    重载友元函数,将first链表输出到输出流对象ost,调用成员函数printList实现。
    (5)printList(p->link)
    递归成员函数printLisk,输出以指针p为头指针的链表各结点所包含的数据到输出流对象ost中。

  • 第5题:

    有以下程序 include void fun(char *t,char *s) { while(*t!=0) t++; while((*t++=*s

    有以下程序

    #include <stdio.h>

    void fun(char *t,char *s)

    { while(*t!=0) t++;

    while((*t++=*s++)!=0);

    }

    main( )

    { char ss[10]="acc",aa[10]="bbxxyy";

    fun(ss,aa); printf("%s,%s\n",ss,aa);

    }

    程序的运行结果是

    A.accxyy,bbxxyy

    B.acc,bbxxyy

    C.accxxyy,bbxxyy

    D.accbbxxyy,bbxxyy


    正确答案:D
    解析:本题中fun函数实现了字符串函数strcat的功能,将字符串aa连接到字符串ss的末尾。调用fun函数时,形参t和s分别指向了字符串ss和aa,然后通过一个while循环使t指向字符串ss的结束符的位置,第二个while循环将字符串aa中的字符(包括结束符“\0”)逐个复制到字符串ss的末尾处。