Category : vector

I wrote the following program, trying to optimize a recursive algorithm using Dynamic Programming. #include <bits/stdc++.h> using namespace std; int mini(int n, vector<int> &memory){ if(n<memory.size()){ return memory[n]; } else{ int m = (n+1)+mini(((n-1)/2), memory)+mini(((n-1)-((n-1)/2)), memory); memory[n]=m; return m; } } int main(){ vector<int> memory={0, 2, 5}; int t; cin >> t; while(t–){ int n; cin ..

Read more

Consider an insert(iterator position, const value_type &x) call: if no reallocation is happening (capacity != size), then in many implementations of the vector I saw this behavior: … template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_insert_aux(iterator __position, const _Tp& __x) #endif { … if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { this->_M_impl.construct(this->_M_impl._M_finish, _GLIBCXX_MOVE(*(this->_M_impl._M_finish – 1))); ++this->_M_impl._M_finish; #ifndef __GXX_EXPERIMENTAL_CXX0X__ ..

Read more

I am currently trying to implement std::vector. The linker can’t find the constructor I wrote. /usr/bin/ld: objs/main.o: in function `main’: main.cpp:(.text+0xed0): undefined reference to `my::vector<int, std::allocator<int> >::vector<int>(int, int, std::allocator<int> const&)’ clang: error: linker command failed with exit code 1 (use -v to see invocation) This is the line in my main.cpp, taken from the example ..

Read more

I have this code that seems to have a problem when calling a vector that uses pointers, Can someone help me on this, it can’t compiled… #include <iostream> #include <vector> using namespace std; const int NUM_COINS = 2; class SuperMario { SuperMario(); void setMapPosition(int x, int y); void getMarioUpdates(); vector <SuperMario*> _mario[NUM_COINS]; }; void SuperMario::setMapPosition(int ..

Read more

I have this code that creates an error – #include <vector> #include <iostream> #include <string> void read_string(std::string &str, std::vector<std::string> &dir, std::vector<std::vector<std::string> > &table, std::vector<std::vector<std::string> > &result) { std::vector<std::vector<std::string> >::iterator it0; std::vector<std::string>::iterator it1; /*intent, iterate over each element (vector of strings) of the vector of elements */ for(it0 = table.begin(); it0 != table.end(); it0++){ /*code to ..

Read more