c++ - Why is replacing an element of a vector so slow? -


i notice amending (or replacing) element in large vector consuming lot of time when vector getting bigger, when element's place in vector known.

is there explanaition this?

i use unsorted set index. code first tries find element in set set.find(). if element not present in set code insert @ end of set , @ same time pushes @ end of vector. if element found on position "x" of set data in vector replaced using:

vector.at(x)=vector[x]+element. 

when skip vector part , insert element in set code processes 95 million elements in less 2 minutes. when add vector part code keeps on running hours.

the file i'm importing semicolumn separated text, below structure

2161182;jutfaseweg;footway;no;7740068,13877901 2953564;timorkade;cycleway;no;7785429,368846814,582743212,582743202,582743213,582743203,582743214,582743206,582743210,45200603 

each line represents way. id's in last element waypoints of particular way. each element has righthand neighbour, unless last element of way , based on 4th element ("yes" or "no", meaning oneway or not), lefthand neighbour, unless first element of way.

below code requested

#include <windows.h> #include <cstring> #include <fstream> #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <cstring> #include <cstdint> #include <cstdio> #include <set>     #include <vector>  using namespace std;  set<string>presentstreet; set<int>presentnode; vector<string>neighbours; string line1;  void split(const string& s, char c,            vector<string>& v) {    string::size_type = 0;    string::size_type j = s.find(c);     while (j != string::npos) {       v.push_back(s.substr(i, j-i));       = ++j;       j = s.find(c, j);        if (j == string::npos)          v.push_back(s.substr(i, s.length()));    } }  int main(int argc, char *argv[]) {   ifstream myfile ("filename.txt");       int counterline=1;   while ( getline (myfile,line1) ) {     string s1=line1;     vector<string> v1;     split(line1, ';', v1);     presentstreet.insert(v1[2]);       vector<string> v2;     split(v1[4], ',', v2);      (int t=0;t<v2.size();t++) {            auto search = presentnode.find(atoi(v2[t].c_str()));           if(search == presentnode.end()) {                      string neighbours="";           if(v1[3].find("no")!=std::string::npos&&t>0) {               neighbours=neighbours+v2[t-1]+",";           }           if(t<v2.size()-1) {             neighbours=neighbours+v2[t+1]+",";           }          stringstream ss;         ss<<counterline;         stringstream ss2;         ss2<<v2[t];                   presentnode.insert(atoi(v2[t].c_str()));         neighbours.push_back(neighbours);       }else{         int nposition = distance (presentnode.begin (), search);         string neighbours=neighbours[nposition];           if(v1[3].find("no")!=std::string::npos&&t>0) {               neighbours=neighbours+v2[t-1]+",";           }           if(t<v2.size()-1) {             neighbours=neighbours+v2[t+1]+",";           }         neighbours.at(nposition)=neighbours;       }     }counterline++;   } } 


Comments