C++语言基础 例程 字符串类

贺老师的教学链接

C++中的新成份——string类型
(1)

#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
    char str1[50],str2[50],temp[50];
    cout<<"please input strings:";
    cin>>str1>>str2;
    if(strcmp(str1, str2)>0)
    {
        strcpy(temp, str1);
        strcpy(str1, str2);
        strcpy(str2, temp);
    }
    cout<<"now, they are: ";
    cout<<str1<<", "<<str2<<endl;
}

(2)

#include <iostream>
#include <string>
using namespace std;
int main( )
{
    string str1,str2,temp;
    cout<<"please input strings:";
    cin>>str1>>str2;
    if(str1>str2)
    {
        temp=str1;
        str1=str2;
        str2=temp;
    }
    cout<<"now, they are: ";
    cout<<str1<<", "<<str2<<endl;
}

(3)

#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
    char *str1, *str2,*temp;
    cout<<"please input strings:";
    cin>>str1>>str2;
    if(strcmp(str1, str2)>0)
    {
        strcpy(temp, str1);
        strcpy(str1, str2);
        strcpy(str2, temp);
    }
    cout<<"now, they are: ";
    cout<<str1<<", "<<str2<<endl;
}

字符串数组

void select_sort(string array[],int n)
{
    int i,j,k,
    string t;
    for(i=0; i<n-1; i++)
    {
        k=i;
        for(j=i+1; j<n; j++)
            if(array[j]<array[k])
                k=j;
        t=array[k];
        array[k]=array[i];
        array[i]=t;
    }
    return;
}
int main()
{
     ……
     select(name, 5);
     ……
}
时间: 2024-09-20 06:36:21

C++语言基础 例程 字符串类的相关文章

C++语言基础 例程 基类与派生类的转换

贺老师的教学链接  本课讲解 指向基类对象的指针变量也可以指向派生类对象 #include <iostream> #include <string> using namespace std; class Student//声明Student类 { public: Student(int, string,float); void display( ); private: int num; string name; float score; }; Student::Student(in

C++语言基础 例程 字符串流

贺老师的教学链接  本课讲解 例:"写"字符数组 #include<iostream> #include <strstream> using namespace std; struct student { int num; char name[20]; float score; }; int main( ) { student stud[3]= {1001,"Li",78,1002,"Wang",89.5,1004,&qu

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 派生类的声明与构成

贺老师的教学链接  本课讲解 派生类 #include <iostream> #include<string> using namespace std; class Student//声明基类Student { public: void sets(int n,string nam,char s); void show( ); protected: //保护部分 int num; string name; char sex ; }; void Student::sets(int n,

C++语言基础 例程 Time类的设计

贺老师的教学链接  本课讲解 Time类的初步实现与测试 #include <iostream> using namespace std; class Time { public: Time(): hour(0), minute(0), sec(0){} Time(int h, int m, int s):hour(h), minute(m), sec(s){} void set_time( ); void show_time( ); void add_a_sec(); //增加1秒钟 voi

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te

C++语言基础 例程 案例:MyVector类的设计

贺老师的教学链接  本课讲解 //MyVector类的设计 #include <iostream> using namespace std; class MyVector //定义向量类 { public: MyVector(int m); //构造函数,共有m个元素的向量,元素值预置为0 MyVector(const MyVector &v); //复制构造函数 ~MyVector(); //析构函数:释放动态数组所占用的存储空间 friend istream &operat

C++语言基础 例程 类和对象的简单应用举例

贺老师的教学链接  本课讲解 实例1:求出三角形的周长和面积 #include<iostream> #include<Cmath> #include<cstdlib> using namespace std; class Triangle { public: void setABC(double x, double y, double z);//置三边的值,注意要能成三角形 double perimeter();//计算三角形的周长 double area();//计算

C++语言基础 例程 类的声明和对象的定义

贺老师的教学链接  本课讲解 类的声明和对象的定义-形式1 #include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( )