C++ “此代码有什么问题?”;长度指标的实施;?

C++ “此代码有什么问题?”;长度指标的实施;?,c++,implementation,indicator,C++,Implementation,Indicator,这是长度指示器字段的一个实现 但它挂起来了,我想它卡在了一个圈里,什么也没显示出来 // readx22.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "iostream" #include "fstream" #include "stdio.h" using namespace std; class Student { public: string i

这是长度指示器字段的一个实现 但它挂起来了,我想它卡在了一个圈里,什么也没显示出来

// readx22.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "stdio.h"
using namespace std;


class Student
{
public:
 string id;
 size_t id_len;
 string first_name;
 size_t first_len;
 string last_name;
 size_t last_len;
 string phone;
 size_t phone_len;
 string grade;
 size_t grade_len;
 void read(fstream &ven);
 void print();
};
void Student::read(fstream &ven)
{
 size_t cnt;
 ven >> cnt;
 id_len=cnt;
    id.reserve( cnt );
    while ( -- cnt ) {
        id.push_back( ven.get() );
    }

 ven >> cnt;
 first_len=cnt;
    first_name.reserve( cnt );
    while ( -- cnt ) {
        first_name.push_back( ven.get() );
    }

 ven >> cnt;
 last_len=cnt;
    last_name.reserve( cnt );
    while ( -- cnt ) {
        last_name.push_back( ven.get() );
    }

 ven >> cnt;
 phone_len=cnt;
    phone.reserve( cnt );
    while ( -- cnt ) {
        phone.push_back( ven.get() );
    }

 ven >> cnt;
 grade_len=cnt;
    grade.reserve( cnt );
    while ( -- cnt ) {
        grade.push_back( ven.get() );
    }

}
void Student::print()
{
// string::iterator it;
 for ( int i=0 ; i<id_len; i++)
  cout << id[i];

}
int main()
{
 fstream in;
 in.open ("fee.txt", fstream::in);
 Student x;
 x.read(in);
 x.print();
 return 0;
}
//readx22.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“iostream”
#包括“fstream”
#包括“stdio.h”
使用名称空间std;
班级学生
{
公众:
字符串id;
尺寸标识长度;
字符串名;
第一排的尺寸;
字符串last_name;
最后的尺寸;
字符串电话;
手机尺寸;
串级;
尺寸等级;
无效读取(fstream&ven);
作废打印();
};
无效学生::读取(fstream和ven)
{
尺寸;
ven>>碳纳米管;
id_len=cnt;
id.reserve(cnt);
而(--cnt){
id.push_back(ven.get());
}
ven>>碳纳米管;
第一个透镜=cnt;
保护区名称(cnt);
而(--cnt){
first_name.push_back(ven.get());
}
ven>>碳纳米管;
最后一列=cnt;
保留地姓氏(cnt);
而(--cnt){
姓氏。向后推(ven.get());
}
ven>>碳纳米管;
电话号码=cnt;
电话预约;
而(--cnt){
电话。推回(ven.get());
}
ven>>碳纳米管;
等级=碳纳米管;
储备等级(cnt);
而(--cnt){
坡度。向后推(ven.get());
}
}
void Student::print()
{
//字符串::迭代器;

对于(int i=0;i如果我正确理解了您的问题,那么您运行此程序时看到的是一个无限循环

我很想知道在每个循环之前cnt是什么

另外,您的代码是否真的可以运行到x.print()



此外,这听起来是一个很好的时机,可以拿出一个调试器并在代码上运行它。如果它是一个无限循环,调试器会很快告诉您卡住的地方。

如果我正确理解了您的问题,您在运行这个时会看到一个无限循环吗

我很想知道在每个循环之前cnt是什么

另外,您的代码是否真的可以运行到x.print()



此外,这听起来是一个在代码上运行调试器的好时机。如果它是一个无限循环,调试器会很快告诉你哪里卡住了。

你可能应该使用
cnt--
而不是
--cnt
。第一个零字节字符串将触发一个非常大的循环,最终导致y会消耗所有内存(可能在64位操作系统上除外)。实际上,甚至不用担心这个修复。循环
get()
效率极低,只需调用
read()

您可能应该在任何地方都使用
cnt--
而不是
--cnt
。第一个零字节字符串将触发一个非常大的循环,最终会消耗所有内存(可能在64位操作系统上除外)。实际上,甚至不用担心这个修复。循环
get()
效率极低,只需调用
read()

您可能希望将所有出现的内容写入控制台以查明问题,例如“cnt”包含的内容。在写入记录时也显示代码,因为从类的声明方式判断,可能认为长度在字符串之后。您可能希望将所有出现的内容写入控制台以查明问题例如,“cnt”包含的内容。在编写记录时也显示代码,因为从类的声明方式来看,人们可能认为长度在字符串之后。是的,我看到无限循环cnt读取字符串的长度。例如:4wine cnt读取4并使用它读取字符串否我认为无限循环存在于x中。read()是的,我看到无限循环cnt读取字符串的长度例如:4wine cnt读取4并使用它读取字符串否我认为无限循环存在于x.read()中