CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Threaded View

  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    adding to vector of vector<int>, check if element already present

    I have a vector<vector<int> > that I am adding data to,
    Code:
       vector<vector<int> > new_set_of_paths;
    
       // to each path1[i], add a neighbor of last_vertex
       for(j=0; j<last_vertex_delta; j++) {
          current_neighbor = vertex_list[last_vertex_in_path].neighbor_numbers[j];
          new_set_of_paths[path_count].push_back( current_neighbor );
          path_count ++;
       }
    I need to do two things.:
    1. check if "current_neighbor" is already a member of new_set_of_paths[path_count][i,j,k]
    2. if it is already a member, delete new_set_of_paths[path_count] from the vector<vector<int>>, slide up the remaining elements, re-size, etc.

    I'm sure I could do this by looping, checking, copying, etc, but I'm sure an algorithm would be better. I sure I could locate using find or find_if, but when I have done this before, I was deleting the element, but in this case, I need to delete the entire vector. This is complicated because the vector is stored in another vector.

    I have tried,
    Code:
       vector<vector<int> > new_set_of_paths;
       vector<int>::iterator path_iterator;
    
       // to each path1[i], add a neighbor of last_vertex
       for(j=0; j<last_vertex_delta; j++) {
          current_neighbor = vertex_list[last_vertex_in_path].neighbor_numbers[j];
    
          // search path to see if new vertex is already present
          path_iterator = find(new_set_of_paths[path_count].begin(),
                               new_set_of_paths[path_count].end(),
                               current_neighbor);
    
             cout << "current_neighbor " << current_neighbor << endl;
             cout << "path_iterator " << path_iterator << endl;
    
          new_set_of_paths[path_count].push_back( current_neighbor );
          path_count ++;
       }
    but this doesn't compile. I am getting
    Code:
    In function `std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > add_vertex_to_path(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, const int&)':
    src/src_client_main/internal_hbond_get_path.cpp:287: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"path_iterator ")) << path_iterator'
    Any suggestions?

    LMHmedchem
    Last edited by LMHmedchem; December 6th, 2011 at 05:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured