Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 04:41

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[c++] help with sub-vector

Discussion on [c++] help with sub-vector within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
[c++] help with sub-vector

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.
Hikarim is offline  
Old 05/19/2014, 16:46   #2
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
std::copy/move or return std::vector(iterator1,iterator2).
Schlüsselbein is offline  
Old 05/19/2014, 18:38   #3
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
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?
Hikarim is offline  
Old 05/19/2014, 18:57   #4

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
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.
Delinquenz is offline  
Old 05/19/2014, 19:22   #5
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
Quote:
Originally Posted by Delinquenz View Post
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.

This is my whole project:
Code:
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;
static int counter=0;

class Tools{
	protected:
		string title;
		int price;
	public:
		Tools(){title=""; price=0;}
		Tools(string title1, int price1){price=price1; title=title1;}
		~Tools(){}
		string toString()const{
			std::stringstream b;
			b << "title: " << title << ", price: " << price;
			return b.str();
		}
};

class Table: Tools{
	public:
	string name;
	int id;
	vector<Tools>myvect;
	public:
	Table(){name=""; id=counter; counter++;}
	Table(string name1, int id1){name=name1; id=id1; counter++;}
	~Table(){myvect.clear();}

	void push(Tools x){
		myvect.push_back(x);
	}
	Tools pop(){
		myvect.pop_back();
		return myvect.back();
	}
	bool delX(int index){
		int x=myvect.size();
		myvect.erase(myvect.begin() + index);
		if(myvect.size()<x){
			return true;
		}
		else{
			return false;
		}
	}
	Tools getX(int index){
		return myvect.at(index);
	}

	int size(){
		return myvect.size();
	}

	string toString()const{
		std::stringstream ss;
		ss << "name: " << name << ", id: " << id << Tools::toString();
		return ss.str();
	}


	void prntout(){
		for(int i=0; i<myvect.size(); i++){
			cout << "name: " << name << ", Id: " << id << ", ";
			cout << myvect[i].toString();
			cout << endl;
		}

	}

	vector<Tools> rtrnSect(int start, int end){
		vector<Tools>subvect;
		for(int i=0; i<(end-start); i++){
			int x=start;
			subvect.at(i)=myvect.at(x);
			x++;
		}
		return subvect;


	}
};


int main() {
	cout << "test0001" << endl;

	Table* t1=new Table("Table1", counter);
	cout << "Num of objects Table:" << counter << endl;
	Tools* c1=new Tools("Com1",300);
	Tools* c2=new Tools("Com2",400);
	Tools* c3=new Tools("Com3",200);

	t1->push(*c1);
	t1->push(*c2);
	t1->push(*c3);
	t1->prntout();

	cout << "---------------" << endl;
	t1->pop();
	t1->prntout();

	cout << "---------------" << endl;
	t1->push(*c3);
	Tools* c4=new Tools("Com4",550);
	t1->push(*c4);
	t1->prntout();
	cout << "---------------" << endl;
	//t1->delX(2);
	if(t1->delX(3)==true){
		cout << "Deleted" << endl;
	}
	else{
		cout << "Not deleted" << endl;
	}
	t1->prntout();

	cout << "---------------" << endl;
	cout << "Size: " << t1->size() << endl;

	cout << "---------------" << endl;
	Table* t3=new Table("Table3", counter);
	cout << "Num of objects Table:" << counter << endl;
	Tools* c5=new Tools("Com5",300);
	Tools* c6=new Tools("Com6",400);
	Tools* c7=new Tools("Com7",200);
	t3->push(*c1);
	t3->push(*c2);
	t3->push(*c3);
	t3->push(*c4);
	t3->push(*c5);
	t3->push(*c6);
	t3->push(*c7);
	t3->prntout();


	return 0;
}
Hikarim is offline  
Old 05/19/2014, 19:26   #6

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
Why do you allocate memory with new?

Instead of this
Code:
	Table* t1=new Table("Table1", counter);
you can write
Code:
	Table t1("Table1", counter);
this.

If you want to continue using new you have to delete everything. That might not be your problem but it's important to know this.

On the other way ideone.com has with your code.

I am not sure if this line
Code:
class Table: Tools{
makes that what you want.
Delinquenz is offline  
Old 05/19/2014, 19:45   #7
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
I am not sure if this line
Code:
class Table: Tools{
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"
Hikarim is offline  
Old 05/20/2014, 13:53   #8

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
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.
Delinquenz is offline  
Old 05/22/2014, 03:13   #9
 
davydavekk's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 101
Received Thanks: 42
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)

Good luck
davydavekk is offline  
Reply


Similar Threads Similar Threads
[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



All times are GMT +1. The time now is 04:42.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.