2013-02-17 5 views
0

이 내 반복자 위치 코드RGD : 벡터 반복자가 얻을 수있는 인덱스 위치

struct node { 
int nodeid; 
vector<fingerTable> fTable; 
vector<string> data; 
}; 

vector<node> cNode; 

vector<node>::iterator position = find(cNode.begin(),cNode.end(), id); 

내가 약 100 개체를 가지고, 나는 가정 "80"예를 들어 nodeid가의 인덱스/요소/위치를 찾기 위해 노력하고 내 객체는 모두 nodeid에 의해 오름차순으로 정렬됩니다.

내 관심은, 내가 그것 .. 그것을 수정하거나 얻기에 어떤 제안이가 더 빨리 듣고 내가 이전에

for(int i=0;i<cNode.size();i++) 
{ 
//if logic-- match nodeid with the nodeid input.. then assign the i to an integer.. 
} 

을 사용했지만 지금은 내가 사용하려고하고 반복자하고 속도와 메모리 사용량입니다 그 값 "nodeid가"

에 의해 내 벡터 인덱스를 찾을 수있는 방법 내가지도가 내 경우에 대한 좋은 표준 컨테이너하지만 아이오와 비트 내가 벡터와 붙어있어 있도록 변경을 할 시간이 부족 알고 ..

vector<node>::iterator position = find(cNode.begin(),cNode.end(), id); 

위의 반복자 행을 컴파일하려고하면 오류 출력이 발생합니다.

In member function ‘void chord::removePeer(int)’: 
testfile.cpp:532:69: error: no matching function for call to ‘chord::find(std::vector<chord::node>::iterator, std::vector<chord::node>::iterator, int&)’ 
testfile.cpp:532:69: note: candidate is: 
testfile.cpp:177:5: note: int chord::find(int, int, bool) 
testfile.cpp:177:5: note: no known conversion for argument 1 from ‘std::vector<chord::node>::iterator {aka __gnu_cxx::__normal_iterator<chord::node*, std::vector<chord::node> >}’ to ‘int’ 
+0

을 기입하셨습니까? 이드의 유형은 무엇입니까? – billz

+0

@billz 나는 을 포함하고, id는 정수이고, 그 고유하고 뚜렷하며 전체 벡터에서 한번만 나타납니다. 오름차순으로 정렬 됨 – user2017011

+0

이 질문은 [어떻게 C++의 데이터로 벡터의 인덱스를 구할 수 있습니까?] (http://stackoverflow.com/q/14914985/78845) 11 시간 전부터 [I 응답 "std :: find()'는 선형 시간 (O (n))에서 실행되며, for 루프와 동일합니다."] (http://stackoverflow.com/a/14917460/78845). 비선형 시간에 실행하려면'std :: lower_bound()'를 사용하십시오. – Johnsyweb

답변

1

당신은 객체의 벡터를 가지고있다. 각 객체는 int를 포함합니다. 당신은 그 int에 주어진 값을 가진 벡터 내의 객체를 "찾으려고"합니다. 그러나 STL은 컨테이너에서 값을 찾는 방법 만 설명하기 때문에 컴파일러는이를 이해하지 못합니다. 어떻게 그렇지 않을 수 있습니까? 두 개의 int를 포함하는 객체가 있다면 어느 것이 비교 될까요?

std::find()을 사용하면 구형 for-loop보다 성능이 향상되었다고 했으므로 지금 시도하지 말고 바로 돌아가십시오. 퍼포먼스는 기본적으로 어느 쪽이든 같을 것이고, 당신은 이미 시간이 없다고 말했습니다. 성능상의 문제가 아니기 때문에 작업 한 것을 사용하십시오. 당신이 반복자를 사용하여 주장하는 경우

, 당신은 당신이 다음과 같이 정의 할 것입니다 사용자 정의 술어 std::find_if()을 사용할 수 있습니다 STL과이 요소를 찾을 수

struct HasId { 
    HasId(int id) : _id(id) {} 
    bool operator()(node const& n) const { return n.nodeid == _id; } 
private: 
    int _id; 
} 

std::find_if(cNode.begin(), cNode.end(), HasId(id)); 

이 방법은, 우리가 제공 한 충분한 정보를 우리 관심있는, 임시 노드를 검색하지 않고.

+0

안녕하세요, find case를 사용하면 어떻게 색인을 찾고있는 벡터 [Index]로 반환 결과를 얻을 수 있습니까? – user2017011

+1

'find_if()'는 반복자를 반환합니다. 'iter'라고 불리우는 경우'* iter'를 사용하여 역 참조를하고 cNode [index]와 같은 결과를 얻을 수 있습니다. 찾은 색인을 알고 싶다면'iter - cNode.begin()'을 실행하십시오. –

+1

왜 색인이 필요합니까? – Thomas

0

cNode는 node 유형의 벡터이다하지만 당신은 ID (INT 타입)에 통과, 당신은 node 객체에 id을 변환하는 암시 적 변환 기능을 필요

struct node { 
    int nodeid; 
    vector<fingerTable> fTable; 
    vector<string> data; 

    node(int id) 
    : nodeid(nodeid) 
    { 
    } 
}; 

bool operator==(const node& lhs, const node& rhs) 
{ 
    return lhs.nodeid == rhs.nodeid; 
} 

는 이제 표준 호출 할 수 있습니다 : : node 벡터에 정수 유형 찾으십시오 :

:

std::vector<node>::iterator position = std::find(cNode.begin(),cNode.end(), id); 

동일하다 C++ 11은 표준과 람다를 작성할 수 :: 같은 다른 방법 find_if와

std::vector<node>::iterator position = std::find(cNode.begin(),cNode.end(), node(id)); 

:

auto pos = std::find_if(cNode.begin(), cNode.end(), 
      [id](const node& n){ return n.nodeid == id; }); 
0

nNode은 벡터가 아닙니다. std::find은 키가 아닌 값을 검색합니다. 노드를 찾으려면 std::map<int,node>과 같은 것을 사용하십시오.

int id = 0; 

typedef std::map<int,node> NodeMap; 
NodeMap cNode; 

NodeMap::iterator position = cNode.find(id); 

당신은 삽입/삭제를 많이 수행하고, 분류 물건을 유지하는 경우, 맵 또는 설정과 같은 적절한 컨테이너를 선택합니다.

이것은 기본적으로 C++ How to speed up my prog design입니다.

struct node { 
    vector<fingerTable> fTable; 
    vector<string> data; 
}; 

다음는 addpeer 정말에만이 수행

map<int,node> cNode; 

를 매핑하는 벡터에서 변경 :

이 가 가

유일하게 남아있는 문제는

void chord::addPeer(int id) 
{ 
    std::map<int, node>::iterator 
    pos = cNode.insert(std::make_pair(id, node())).first;; 

    if(pos != cNode.end()) 
    { 
     ++pos; 
     vector<string> data = pos->second.data; 
     pos->second.data.clear(); 
     dataShift(data, fIndex-1); 
    } 
}//end addPeer 
당신이 노드를 변경하는 경우

dataShift은 무엇을하고 색인을 필요로합니까?

관련 문제