标题:练习了一下vector推入类的简单例子,结果循环不了,求大神看一下咋回事
只看楼主
powers0010
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-5-20
结帖率:33.33%
已结贴  问题点数:20 回复次数:3 
练习了一下vector推入类的简单例子,结果循环不了,求大神看一下咋回事
程序代码:
#pragma once

#include<cstring>
#include<iostream>
using std::cout;
using std::endl;

class person
{
public:
    person(const char * first, const char * second);
    person(const person & p1);
    person & operator =(const person & p1);
    bool operator <(const person & p1) const;
    void show() const;
    void initname(const char * first, const char * second);
    ~person();

private:
    char * firstname;
    char * secondname;
};


程序代码:
#include "StdAfx.h"
#include "person.h"


person::person(const char * first, const char * second)
{
    initname(first,second);
}
person::person(const person & p1)
{
    initname(p1.firstname,p1.secondname);
}
person & person::operator =(const person & p1)
{
    delete [] firstname;
    delete [] secondname;
    initname(p1.firstname,p1.secondname);
    return *this;
}
bool person::operator <(const person & p1) const
{
    if(strcmp(this->firstname,p1.firstname)<0||((strcmp(this->firstname,p1.firstname)==0)&&(strcmp(this->secondname,p1.secondname)<0)))
        return 1;
    return 0;
}
void person::show() const
{
    cout<<firstname<<" "<<secondname<<endl;
}
void person::initname(const char * first, const char * second)
{
    unsigned int len=strlen(first)+1;
    this->firstname=new char(len);
    strcpy_s(firstname,len,first);
    len=strlen(second)+1;
    this->secondname=new char(len);
    strcpy_s(secondname,len,second);
}
person::~person(void)
{
    delete [] firstname;
    delete [] secondname;
}


程序代码:
// vestor01.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#include<iostream>
#include"person.h"
#include<vector>
using std::vector;
using std::cin;

int main()
{
    vector<person> people;
    const size_t maxlength=50;
    char firstname[maxlength];
    char secondname[maxlength];
    while(1)
    {
        cout<<"请输入姓(或enter终止):";
        cin.getline(firstname,maxlength ,'\n');
        if(strlen(firstname)==0)
            break;
        cout<<"请输入名:";
        cin.getline(secondname,maxlength ,'\n');
        //people.push_back(person(firstname,secondname));
        person p(firstname,secondname);
        people.push_back(p);
    }
    cout<<endl;
    auto iter=people.begin();
    if(iter!=people.end())
        iter++->show();
    return 0;
}
搜索更多相关主题的帖子: person second 
2017-05-20 15:49
powers0010
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-5-20
得分:0 
跑起来没问题,就是循环直接卡住了。。。
2017-05-20 15:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
一直听不懂“就是循环直接卡住了。。”是什么意思,就没法回答

两处 new char(len) 改为 new char[len];
其它错误就懒得说了,比如 operator = 竟然不判断自己给自己赋值的情况。
2017-05-22 08:24
powers0010
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-5-20
得分:0 
回复 3楼 rjsp
[em19给大神撒花,I get it
2017-05-22 14:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-477183-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.132104 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved