【发布时间】:2015-02-17 15:51:46
【问题描述】:
我已经搜索了整个 stackoverflow,但似乎无法找到我正在寻找的东西,所以这里是.. 我有 2 个自定义类,一个在第二个中使用,当我尝试对第二个类进行排序时。它本质上是第一个类的向量,它会引发错误:-消息 0x010c3e18“向量迭代器不兼容”const wchar_t *at vc\include\vector 第 238 行 .. 这是我的代码:
#include <iostream>
#include <conio.h>
#include <cmath>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
class cityPhone {
private:
string cityName;
string cityCode;
public:
void setCode(string code){
cityCode=code;
}
void setName(string name){
cityName=name;
}
cityPhone(){
cityName="Varna";
cityCode="0888123123";
}
cityPhone(string name, string code){
cityName=name;
cityCode=code;
}
string getCity(){
return cityName;
}
string getCode(){
return cityCode;
}
};
//struct { //ive used these 2 also and still it doesnt work
// bool operator()(cityPhone a, cityPhone b)
// {
// if (a.getCity().compare(b.getCity())>0)return true;
// return false;
// }
// } cmpCity;
// struct {
// bool operator()(cityPhone a, cityPhone b)
// {
// if (a.getCode().compare(b.getCode())>0)return true;
// return false;
// }
// } cmpCode;
bool cmpCity(cityPhone a, cityPhone b) // i'm using these 2 in the sorting method
{
if (a.getCity().compare(b.getCity())>0)return true;
return false;
}
bool cmpCode(cityPhone a, cityPhone b)
{
if (a.getCode().compare(b.getCode())>0)return true;
return false;
}
class phoneDirectory {
private :
vector<cityPhone> data;
public:
phoneDirectory (string path){
read(path);
}
phoneDirectory (){
data=vector<cityPhone>();
}
void read(string path){
cout<<endl;
try {
string line;
ifstream myfile (path);
cityPhone bla = cityPhone();
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
try{
bla = cityPhone(line.substr(0,line.find_first_of(" ")),line.substr(line.find_first_of(" ")+1));
data.push_back(bla);
}
catch(exception){ }
}
myfile.close();
}
else cout << "Unable to open file";
} catch (exception) {}
}
void addCityPhone(string city,string phone){
try{
data.push_back(cityPhone(city,phone));
}
catch(exception){
cout<<"Error adding item "<<endl;
}
}
void delCityPhone(int index){
try{
vector<cityPhone>::iterator p=data.begin();
p+=index;
data.erase(p);
}
catch(exception){
cout<<"Error deleting item with index "+index<<endl;
}
}
cityPhone getCityPhone(unsigned index){
try{
vector<cityPhone>::iterator p=data.begin();
p+=index;
return *p;
}
catch(exception){
cout<<"Error deleting item with index "+index<<endl;
return cityPhone();
}
}
vector<cityPhone> getData(){
return data;
}
void phoneChange(string city, string newPhone){
try{
int i=0;
vector<cityPhone>::iterator p=data.begin();
for(p=data.begin();p<data.end();p++,i++){
if (getCityPhone(i).getCity().compare(city)==0){
string oldPhone = getCityPhone(i).getCode();
getCityPhone(i).setCode(newPhone);
cout<<"Phone of city "+city + " was changed from "+oldPhone + " to " + newPhone<<endl;
return;
}
cout<<"No such city exists!\n";
}
}
catch(exception){
cout<<"Error changing phone"<<endl;
}
}
friend istream& operator>>(ostream& out,phoneDirectory a);
friend ostream& operator<<(ostream& out,phoneDirectory a);
};
istream& operator>>(istream& in,phoneDirectory& a){
string city,phone;
in >> city >> phone;
a.addCityPhone(city,phone);
return in;
}
ostream& operator<<(ostream &out, cityPhone a){
return out << a.getCity() <<" " << a.getCode() <<endl;
}
void sortByCity(phoneDirectory a){//with these two i try to sort
vector<cityPhone>::iterator p=a.getData().begin();
vector<cityPhone>::iterator q=a.getData().end();
std::sort(p,a.getData().end(),cmpCity);
}
void sortByCode(phoneDirectory a){
vector<cityPhone>::iterator p=a.getData().begin();
vector<cityPhone>::iterator q=a.getData().end();
std::sort(p,q,cmpCode);//i've tried with std::sort(a.getData().begin(),a.getData().end(),cmpCode) still no effect
}
//bool wayToSortCity (cityPhone a, cityPhone b){ //i've tryed also with these , still no effect
//if (a.getCity().compare(b.getCity())>0)return true;
//return false;
//}
// bool wayToSortCode (cityPhone a, cityPhone b){
//if (a.getCode().compare(b.getCode())>0)return true;
//return false;
//}
int main()
{
phoneDirectory test("C:\\t.txt");
sortByCity(test);//here it crashes
for(unsigned i=0;i<test.getData().size();i++)
cout<<test.getCityPhone(i);
cin>>test;
//test.sortBy(1);
system("pause");
return 0;
}
【问题讨论】:
-
请显示 complete 和 unedited 错误输出。并在您的代码中标出错误所在的位置。
-
在修复了几个小问题(但很明显)后,我无法重现您的错误。
标签: c++ sorting vector stl iterator