因此,我將對象放置在矢量中。我想在添加它們時按順序放下它們。對象的基礎是向量中的字符串。並將它們按順序排列
class myObj {
private:
string firstName;
string lastName;
public:
string getFirst;
string getLast;
}
我也有這些對象
vector<myObj> myVect;
vector<myObj>::iterator myVectit = myVect.begin();
的載體,當我添加一個新的對象,我想找到它應該插入之前被放置在載體。我可以通過對象值搜索矢量嗎?這是我的第一次嘗試
void addanObj (myObj & objtoAdd){
int lowwerB = lower_bound(
myVect.begin().getLast(), myVect.end().getLast(), objtoAdd.getLast()
);
int upperB = upper_bound(
myVect.begin().getLast(), myVect.end().getLast(), objtoAdd.getLast()
);
從那裏我打算使用lowwerB和上面的B來確定插入條目的位置。我需要做些什麼來實現這個目標,或者什麼是解決這個挑戰的更好方法?
----跟進----
錯誤,我得到當我試圖編譯
error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
No user-defined-conversion operator available that can perform this conversion,
or the operator cannot be called
編譯器突出了LOWER_BOUND和UPPER_BOUND。我猜想它指的是我把
objtoAdd.getLast()
其中-----更多跟進----------------- 這是接近,但編譯不完全的。我應該期望從lower_bound和upper_bound獲得什麼?它不匹配我定義的迭代器,我不知道我應該期待什麼。
void addMyObj(myObj myObjtoadd)
vector< myObj>::iterator tempLB;
vector< myObj>::iterator tempUB;
myVectit= theDex.begin();
tempLB = lower_bound(
myVect.begin()->getLast(), myVect.end()->getLast(), myObjtoadd.getLast()
);
tempUB = upper_bound(
myVect.begin()->getLast(), myVect.end()->getLast(), myObjtoadd.getLast()
);
你有什麼理由使用'vector'嗎?看起來你可能想要一個'std :: map'或'std :: multimap',當你調用'insert'時,它將返回一個指向插入對象的迭代器。它也將保持一切爲你排序。 – druckermanly 2014-11-05 03:12:37
由於沒有邏輯關鍵與價值,我認爲你的意思是設置,而不是地圖。 – chris 2014-11-05 03:16:33
此外,請參閱此處:http://stackoverflow.com/questions/15843525/how-do-you-insert-the-value-in-a-sorted-vector – msandiford 2014-11-05 03:21:41