我想按照字母顺序从结构体打印字符串,并且我从线程How to alphabetically sort strings?获得了排序帮助。我的问题是,当我运行编译器我得到一个排序的输出,但它包含另一个结构的名称。我的代码如下所示:以字母顺序打印结构体输入C++
#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
const int antalShops = 2;
const int antalWorkers = 5;
struct employ {
string workerName; int workerAge;
};
struct theMall{
string shopName; string shopType; int shopSize;
employ workerName; employ workerAge;
};
// Declaration of the structs
theMall Shops[antalShops] = {
{"GameStop","toy", 250,},
{"Frandsen", "cloth", 300,},
};
employ Workers[antalWorkers] = {
{"Andrea valente", 41},
{"Giovanni Pirolli", 25},
{"Marco Cipolli", 33},
{"Jensine Jensen", 19},
{"Andrea Jensen", 99},
};
// Functions for sorting and printing names
void print(const string& item) {
cout << item << endl;
}
void PrintWorkers(employ Workers[]) {
set<string> sortedWorkers;
for(int i = 0; i <= antalWorkers; ++i) {
sortedWorkers.insert(Workers[i].workerName);
}
for_each(sortedWorkers.begin(), sortedWorkers.end(), &print);
}
void PrintShops(theMall Shops[]) {
set<string> sortedShops;
for (int i = 0; i <= antalShops; ++i) {
sortedShops.insert(Shops[i].shopName);
}
for_each(sortedShops.begin(), sortedShops.end(), &print);
}
int main(int argc, const char * argv[])
{
PrintShops(Shops);
}
所以我有工人和商店的结构,但是当我我尝试用PrintShops(Shops)
功能打印店的名字我得到的输出:
Andrea Valente
Frandsen
GameStop
我有一直在查看代码,但我找不到错误在哪里,任何人都可以看到错误?
对于变量类型和大写使用小写字母标识符非常混乱。 –
为什么不使用['std :: sort'](http://en.cppreference.com/w/cpp/algorithm/sort)对数组进行排序?或者将它们保存在一个['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)中,并且如果你不想修改原始文件,将它们复制到你想要的位置容器。 –
你永远不会初始化你的任何'theMall'结构中的两个'雇员'成员。 –