- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
好的,下面是我还需要做的事情。我在语法方面遇到了一些严重的问题。我不知道为什么在尝试声明动态分配的数组时会出现错误。如果我能在这方面得到一些帮助,那就太好了。
BabyNames
类:
<<
) 和提取 (>>
) 运算符。 这是标题
/*
* File: BabyNames.h
* Author: jack
*
* Created on March 18, 2013, 3:35 PM
*/
#ifndef BABYNAMES_H_
#define BABYNAMES_H_
#include <string>
#include <fstream>
using namespace std;
using std::ifstream;
/**
* gender: an enum to hold types boy and girl
*/
enum gender {boy, girl};
/**
* PeopleStats: a data struct to hold year, name, double, and the enum type
* gender
*/
struct PeopleStats {
short year;
string name;
double percent;
gender sex;
};
/**
* Names: A class that reads data from a file into a PeopleStats Array
* and is capable of sorting them by name and by popularity percentage
*/
class BabyNames {
public:
BabyNames();
BabyNames(const char fn[], const int numLines);
gender parseSex(string s);
void swap(int i);
const int getCapacity();//getter for dataSize
const PeopleStats& get(int i) const; //getter for objects of the data array
void sortByName(); //Sorts the names alphabetically
void sortByPopularity();//Sorts the names by popularity
friend std::ostream& operator<<(std::ostream& os, const BabyNames& bn);
//friend std::PeopleStats& operator =(const PeopleStats& rhs);
private:
static const int MAX_DATA_SIZE = 25000;
PeopleStats* people;
int capacity;
int fillCount; // to keep track of how many array locations have data objects in them
static const int OUT_OF_BOUNDS = -1;
};//end of class
#endif /* BABYNAMES_H */
这里是函数
/**
* PeopleSort.cpp
* Description:
* This class stores the information in data structure and includes methods that
* sort the data by name and by popularity.
* Created:
* Feb 27, 2013
* Modified:
* March 1st 2013
*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
using std::ifstream;
#include "BabyNames.h"
/**
* BabyNames: A class that reads data from a file into a PeopleStats Array
* and is capable of sorting them by name and by popularity percentage
*/
BabyNames::BabyNames(const char fn[], const int numLines) {
people = new PeoplStats[MAX_DATA_SIZE]; //edit I put this in but I still get errors
//set current capacity
capacity = numLines;
//Open File
ifstream myfile;
myfile.open(fn);
//read in data
int current = 0;
while (current < capacity) {
myfile >> people[current].year;
myfile >> people[current].name;
myfile >> people[current].percent;
string sex;
myfile >> sex;
people[current].sex = parseSex(sex);
current++;
}
}
BabyNames::~BabyNames(){
cout << "Deconstructor" << endl;
delete[]people; //Edit: I added this
}
/*
* sortByName - Sorts the entire array of people by name using the bubble
* sort algorithm. Array is sorted according to name, within the structure
* Algorithm becomes extremely slow if capacity > 3000
*/
void BabyNames::sortByName(){
//sorted is set to true if no changes were made
bool sorted = false;
//change tells the loop if we made a swap or not
bool change = false;
while (!sorted) {
for (int i = 0; i < capacity - 1; i++) {
int compare = people[i].name.compare(people[i + 1].name);
if (compare == 1) {
swap(i);
change = true;
}
}
//if the array was not changed,
//changed sorted to true and exit loop
if (change == false)
sorted = true;
//otherwise reset change and repeat the loop
else
change = false;
}
}
/**
* sortByPopularity - sorts the entire array using the bubble sort algorithm
* Method is almost exactly the same as sortByName, except the compare
* variable is a double consisting of the difference of the two popularity
* percentages.
* Algorithm becomes extremely slow if capacity > 3000
*/
void BabyNames::sortByPopularity(){
bool sorted = false;
bool change = false;
while (!sorted) {
for (int i = 0; i < capacity - 1; i++) {
double compare = people[i].percent - people[i + 1].percent;
if (compare < 0) {
swap(i);
change = true;
break;
}
}
if (change == false)
sorted = true;
change = false;
}
}
/**
* swap - swaps the positions of people[i] and people[i + 1]
* @param i - index of the people array that will get swapped with i + 1
*/
void BabyNames::swap(int i) {
PeopleStats temp = people[i + 1];
people[i + 1] = people[i];
people[i] = temp;
}
/**
* parseSex: extracts the gender from the input and returns the appropriate enum
* @param s
* @return gender object
*/
gender BabyNames::parseSex(string s) {
if (s == "boy")
return boy;
else if (s == "girl")
return girl;
else
throw "invalid Gender";
}
/**
* getCapacity: returns the number of data entries stored.
* @return size of the data
*/
const int BabyNames::getCapacity(){
return capacity;
}
/**
* get: returns one PeopleStat Object
* @param i
* @require: i < capacity
* @return PeopleStat object
*/
const PeopleStats& BabyNames::get(int i) const {
if (i>=0 && i < capacity)
return people[i];
throw OUT_OF_BOUNDS;
}
/**
* << - overloads the outstream operator for PeopleStats object
* @param os
* @param d
* @return os : output stream
*/
std::ostream & operator << (std::ostream& os, const PeopleStats& d){
os << d.name << " " << d.year << " " << d.percent << " " << d.sex;
return os;
}
/**
* <<: overloads the outstream operator for the BabyNames object
* @param os
* @param bn
* @return
*/
std::ostream& operator<<(std::ostream& os, const BabyNames& bn) {
os << " BabyNames object " << std::endl;
for (int i=0; i < 5000; i++ )
os << " " << (i+1) <<": " << bn.get( i ) << std::endl;
return os;
}//end of operator
这是主要内容
/*Title: CS 237 Assignment#1
* Created:
* Feb 27, 2013
* Modified:
* March 1st 2013
********************************/
#include <iostream>
#include "BabyNames.h"
/**
* main: The client function that controls the server, BabyNames
* For reasons currently unknown, the program will take forever
* to run after the * 3000-5000 line mark.
* @return 0 if successful
*/
int main(void) {
cout << "People names: " << endl;
BabyNames names("src/people.txt", 5000 );
cout << names;
cout << "Sorted by popularity" << endl;
names.sortByPopularity();
cout << names;
// cout << "Sorted by name" << endl;
// names.sortByName();
// cout << names;
cout << "... done!" << endl;
return 0;
//This is a suggested Main by my professor
/* cout << "People names: " << endl;
const char filename[] = "src/people.txt";
cout << " 1. Reading file: \"" << filename << "\" ... ";
int number_of_names = checklines( filename );
cout << " number of lines in file is: " << number_of_names;
BabyNames* names = new BabyNames( number_of_names );
// read the contents of a file into names and print them
ifstream file( filename, ios::in );
file >> *names;
cout << " file has been read!" << endl;
// Sort and print
cout << " 2. Sorted by popularity:" << endl;
BabyNames* namesByPop = new BabyNames( *names );
namesByPop->sortByPopularity();
cout << *namesByPop;
// Sort and print
cout << " 3. Sorted by name:" << endl;
BabyNames* namesByName = new BabyNames( *names );
namesByName->sortByName();
cout << *namesByName;
// Sort and print
cout << " 4. Sorted by year:" << endl;
BabyNames* namesByYear = new BabyNames( *names );
namesByYear->sortByYear();
cout << *namesByYear;
cout << " 5. Original names:" << endl;
cout << *names;
delete names;
delete namesByYear;
delete namesByName;
delete namesByPop;*/
cout << "... all done!" << endl;
}
此外,我正在阅读的文件中有很多行看起来像这样
1880 John 0.081541 boy
1880 William 0.080511 boy
1880 James 0.050057 boy
1880 Charles 0.045167 boy
最佳答案
我想我没有看到您在构造函数中实际分配内存的位置。
BabyNames::BabyNames(const char fn[], const int numLines) {
people = new PeopleStats[numLines];
// check for null
.....
}
此外,释放析构函数中的内存:
BabyNames::~BabyNames() {
delete [] people;
}
关于c++ - 需要动态数组、析构函数、深度复制帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549929/
我正在编写一个应用程序,允许用户创建一个“问卷”,然后向其中添加问题。我正在使用核心数据来存储信息。我创建了一个问卷实体,并与问题实体建立了“一对多”关系。我的问题是,如果要允许用户复制(复制)整个调
有没有办法复制或复制 SharedPreference?或者我需要从一个变量中获取每个变量,然后将它们放入另一个变量中吗? 最佳答案 尝试这样的事情: //sp1 is the shared pref
下面的(A)和(B)有区别吗? (假设 NON ARC,如果重要的话) // --- (A) --- @interface Zoo : NSObject{} @property (copy) Dog
我正在尝试将 mysql SELECT 查询保存到文件中,如下所示: $result = mysqli_query($db,$sql); $out = fopen('tmp/csv.csv', 'w'
我需要创建一个 CVPixelBufferRef 的副本,以便能够使用副本中的值以按位方式操作原始像素缓冲区。我似乎无法使用 CVPixelBufferCreate 或 CVPixelBufferCr
我在 Source 文件夹中有一个 Active wave 录音 wave-file.wav。我需要使用新名称 wave-file-copy.wav 将此文件复制到 Destination 文件夹。
在使用 GNU Autotools 构建的项目中,我有一个脚本需要通过 make 修改以包含安装路径。这是一个小例子: configure.ac: AC_INIT(foobar, 1.0) AC_PR
我想将 SQL 的行复制到同一个表中。但是在我的表中,我有一个“文本”列。 使用此 SQL: CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT
谁能给我解释一下 df2 = df1 df2 = df1.copy() df3 = df1.copy(deep=False) 我已经尝试了所有选项并执行了以下操作: df1 = pd.DataFram
Hazelcast 是否具有类似于 Ehcache 的复制? http://www.ehcache.org/generated/2.9.0/pdf/Ehcache_Replication_Guide.
我有以下拓扑。一个 Ubuntu 16.04。运行我的全局 MySQL 服务器的 Amazon AWS 上的实例。我想将此服务器用作许多本地主服务器(Windows 机器 MySQL 服务器)的从服务
使用 SQLyog,我正在测试表中是否设置了正确的值。我尝试过 SELECT type_service FROM service WHERE email='test@gmail.com' 因此,只输出
有人可以提供一些关于如何配置 ElasticSearch 进行复制的说明。我在 Windows 中运行 ES,并且了解如果我在同一台服务器上多次运行 bat 文件,则会启动一个单独的 ES 实例,并且
一 点睛 ThreadGroup 复制线程的两个方法。 public int enumerate(Thread list[]) // 会将 ThreadGroup 中的 active 线程全部复制到
一 点睛 ThreadGroup 复制线程组的两个方法。 public int enumerate(ThreadGroup list[]) // 相对于 enumerate(list,true) pu
官方documentation Cassandra 说: Configure the keyspace and create the new datacenter: Use ALTER KEYSPAC
This question already has answers here: How to weight smoothing by arbitrary factor in ggplot2? (2个答
我们有一个表格来表明对各种俱乐部的兴趣。输出将数据记录在 Excel 电子表格中,其中列有他们的首选姓名、姓氏、电子邮件、代词,以及他们感兴趣的俱乐部的相应列中的“1”(下面的模型)。 我们希望为俱乐
This question already has answers here: Closed 8 years ago. Possible Duplicate: In vim, how do I get
如何复制形状及其所在的单元格?当我手动复制时,形状会跟随单元格,但是当我使用宏进行复制时,我会得到除形状之外的所有其他内容。 Cells(sourceRow, sourceColumn).Copy C
我是一名优秀的程序员,十分优秀!