|
-
December 7th, 2011, 12:30 AM
#7
Re: adding to vector of vector<int>, check if element already present
Thanks, I'm making some progress. I am working under the assumption that I can use the individual row of my vector<vector<int>> as I would any container,
Code:
path_iterator = find(new_set_of_paths[path_count].begin(),
new_set_of_paths[path_count].end(),
current_neighbor);
meaning that I don't have to copy out the data into a regular vector<int>.
It is working more or less, but I need to remove rows of the <vector<int>> that I'm not going to add to. Right now, it is crashing because the rows are still there. This code searches the current row of new_set_of_paths[] to see if a vertex is already present in the path. If the vertex is not already there, it is added, if not it is skipped.
Code:
vector<vector<int> > new_set_of_paths;
if(new_set_of_paths.size() != 0) {
// iterator for vector search
vector<int>::iterator path_iterator;
// to each path1[i], add a neighbor of last_atom
for(j=0; j<last_vertex_delta; j++) {
// lookup value of next neighbor to possibly add
current_neighbor = vertex_list[last_vertex_in_path]neighbor_numbers[j];
// check to make sure that any added vertex is not already in the path
// prevents moving backwards and through ring closures
path_iterator = find(new_set_of_paths[path_count].begin(),
new_set_of_paths[path_count].end(),
current_neighbor);
// new verticies are added to path_temp[path_count] because not all
// verticies are added and the total is cumulative over all branches
if(path_iterator != new_set_of_paths[path_count].end()) {
cout << "don't add " << current_neighbor << endl;
} else {
cout << "do add " << current_neighbor << endl;
new_set_of_paths[path_count].push_back( current_neighbor );
path_count ++; //
}
}
}
I need to add code to the don't add part of the conditional that would remove new_set_of_paths[path_count] row from the new_set_of_paths vector. It's more complex than that, because I actually don't want removal in every case, but I do need to figure out the syntax for removal when I do need it.
I have tried,
Code:
int erase_at = path_count-1;
new_set_of_paths.erase(new_set_of_paths.begin()+erase_at);
but I'm not sure that's right. I need to work a bit on when to do this and when not to because I'm sure it will go boom if I don't pay attention to that. I know erase can foul up iterator values, but I'm not sure it that is an issue here or not.
LMHmedchem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|