Well, i have a vector that stores objects of my class named 'Tools':
vercotr<Tools>myvect
that has lets say 5 objects saved in it... But now i want to make a method that makes a subvector from lets say 2nd (start) to 4th (end/finish) element in myvect and returns that newly created subvector.
returnSubvect(2,4); that is returning: subvect
Can anyone explain me how to do this? -Was strugling with this for some time now, but i cant get it to work.
I did i like this, but it kinda loops and times out when i try to run..
Code:
vector<Tools> retrnSect(int start, int end){
vector<Tools>subvect;
for(int i=0; i<(end-start); i++){
int x=start;
subvect[x]=myvector[x];
x++;
}
return subvect;
}
My toughts behind this was: i calculate the starting position and the endpostion and insert those elements from old vector (myvector) to the new one (subvect).
The thing compiles fine, but when i run it, it loops and breaks...
You mind checking what i am doing wrong?
Avoid using the [] - it's not good practice. Instead try using std::vector::at(). Schlüsselbein talked about iterators. - why are you using random access? It's really slow if you compare it with iterators. In my opinion iterators also are easier to understand.
Avoid using the [] - it's not good practice. Instead try using std::vector::at(). Schlüsselbein talked about iterators. - why are you using random access? It's really slow if you compare it with iterators. In my opinion iterators also are easier to understand.
Didn't know that, because im just learning and i missed last weeks lesson at the school, because i was sick, so now im trying to catch up Thanks for the tips.
I tryed with .at() its basically the same as [], but the problem is that when i try to run the program, it collapses at the point when i call the method 'test0801.exe has stopped working' im using eclipse btw.
makes that what you want.[/QUOTE]
Ups, my tipo, should be class Table: public Tools... (using inheritance..)
I'm using new to allocate memory, because the school wants me to make my objects dynamically... i know i have to delete them but i didnt get to that part as i have other bugs atm...
it isnt anything wrong because i removed the parth when i try to call the method rtrnSect that has to reterun me a new vector that contains a sub region of my original vector....
am i calling the method wrong?
Code:
t3->rtrnSect(2,4);
gives me this: "This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std:ut_of_range'
what(): vector::_M_range_check"
Why should your Table class inherit from your Tools class when it stores them? I can't follow your logic :S In my opinion this is completely wrong, but it has nothing to do with your error. Anyway, better fix this issue.
If your application throws a std:: out_of_range exception you tried to get access to entries which the vector doesn't have. You should check the size of the sector first with the std::vector<T>::size() method to avoid such logical errors. That's the advantage of std::vector::at() since it throws exceptions if you try to access entries which don't exist instead of aborting with a dubious memory error.
Here is an example with std::vector<int>, I'll let you modify it so it can fit your needs.
Code:
std::vector<int> subVector(const std::vector<int>& vec, int beg, int end)
{
end++; // you can remove that if you don't want the element at index "end" to be included
if (end <= vec.size()) // size check
{
std::vector<int> subVec;
std::copy(vec.cbegin() + beg, vec.cbegin() + end, std::back_inserter(subVec));
/* std::back_inserter is an iterator that calls push_back() to insert elements at the end of std containers */
return subVec;
}
else
return std::vector<int>(); // returns an empty vector
}
Example :
Code:
std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<int>& v2 = subVector(vec, 0, 2); // remember that vectors are 0-indexed
for (auto it = v2.cbegin(); it != v2.cend(); it++)
{
std::cout << *it << ' '; // prints 1 2 3
}
std::cout << std::endl;
By the way, your OOP design is horrible.
-If your class Table needs to access Tools' private members, make it a friend class.
-Don't use dynamic memory allocation if you don't know how it works ... here you are leaking* every single object you allocated, that's not good.
leaking : When an object is somewhere on the heap, but you don't have any valid pointer to it. (which means that you can't deallocate it)
[REQUEST] VECTOR 09/05/2013 - Facebook - 3 Replies anyone know any hack gold / coins or stars for the next game facebook?
yes?
LINK:
https://apps.facebook.com/vectorthegame/?fb_sourc e=canvasbookmark_recommended
vector hack 06/18/2013 - Facebook - 1 Replies vector hack
open charles
open vector
end any misson
'fb.vectorgames.ru/php/vl_api2.php' find it
Vector out of bounce 06/05/2013 - C/C++ - 7 Replies try
{
return MapContainer.at(index) ;
}
catch (std::exception& e)
{
std::cout << "Element " << index << ": index exceeds vector dimensions." << std::endl;
}
Vector Problemchen 03/02/2013 - C/C++ - 3 Replies int _tmain(int argc, _TCHAR* argv)
{
string lvlname;
char option;
ifstream input;
ofstream output;
cout << "Geben sie den Namen ein."<<endl;
cin >> lvlname;
Mw2 Vector 05/12/2011 - Call of Duty - 9 Replies Stimmt das wenn man Schaldämpfer raufschrauft dass sich die Vector nicht mehr so verzieht