Resizeing vector, out_of_range

I have an application where I need to reallocate vectors, as they tend to run out of room.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
vector<int> myHits(10000);
  int i;
  int count = 0;
  for (vector<string>::size_type i = myStart; i <= myEnd; i++) {
    //Search forward
    if(count >= myHits.size()){
      myHits.resize(1000);
    }
    if(ref.find(searchVector.at(i)) != std::string::npos){
      myHits.at(count) = ref.find(searchVector.at(i));
    } else if (ref.find(reverseString(searchVector.at(i))) !=
std::string::npos ){
      myHits.at(count) = ref.find(reverseString(searchVector.at(i)));
    } else {
      // myHits.at(i) = 0;
);
    }
    count++;
  }
 
 
  pthread_mutex_lock(&mutexDNS);
  Hits.resize(1001);
  std::vector<int>::iterator it = Hits.begin();
  Hits.insert(it, myHits.begin(), myHits.end());
  pthread_mutex_unlock(&mutexDNS);


I get out of range. When I allocate a vector<int> of just some element in size the program stops immediately throwing out_of_range error. If I allocate a 10000 sized vector it runs for a while. What I want to do is allocate whenever myHits runs out of room. What am I doing wrong?
Why are you doing that!? Vectors will resize and reallocate themselves for you when you use push_back(). You can request capacity ahead of time with reserve(), but you should allow it to resize itself always - otherwise there is no benefit from it over a dynamic array with new[] and delete[].
Last edited on
LB - I know, I used push_back but I thought push_back was hurting performance so I wanted to compare with reallocating a vector, or resize if you will. Is this all wrong..? I jam in some 1000 elements in each myHits/thread.
Last edited on
You should use reserve() to tell the vector that you are planning on having that many elements. You should not use resize().
Topic archived. No new replies allowed.