0

Hello there;
I am new to object oriented programming need to use vectors. basically I have data from CSV file and I stored it in a vector. I need to partition this vector to n vectors and I need it to be dynamic.
I started using class in order to create vector of dynamic objects ( ihope it is right) but I am confuse how can I use vector.

cluster.h

#ifndef __CLUSTER_H
#define __CLUSTER_H

#include <iostream>
#include <string>
#include<vector>

using namespace std;

class Cluster
{

private:
	static int thershold;
	vector <int> v;	//will store cluster of values each time called
	int centre;		//centre of gravity to a cluster

	//vector <vector <int> > m;//define array of vectors 

public:
	Cluster(){ }
	//~Cluster();*/
	void getdata(vector<int> &TKoffice);//get from m1 first  //friend 
	void putdata();
	bool add_member(int point);
		
};

#endif	// __Cluster_H

Cluster.cpp

#include "Cluster.h"
#include<iostream>
using namespace std;

//define static data **** without writing static in the begining
int Cluster::thershold=30;

bool Cluster::add_member(int point)
{
	if (v.size() == 0)
	{
		v.push_back(point);
		centre=point;
		return true;
	}
	else if (abs(point-centre)<= Cluster::thershold) //if <= 30min
	{
		v.push_back(point);
		for (int i=0; i<v.size(); i++)
		{
			//compute the new centre
			//calculate the average
			//and update the centre value
			//return true?
		}
	}
	else
		return false;
}



void Cluster::putdata()// is it right ??
{
	cout<<"=============="<<"\n";
	for (int i=0;i<v.size();i++)
     cout <<v[i]<<endl;
}

//main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "Cluster.h"
using namespace std;



//Convert csv strings to integer
int string_to_int(const string& s)
{

    istringstream instr(s);    
    int val;
    instr >> val;
    return val;
	
}
void Data_into_vectors(ifstream& in,vector<int>& TimeX,vector<int>& LocY)
{
	string line;
    int pos;	
    int row=0;
    int row2=0;
	int st2int;
    if(!in.is_open())
		cout << "Failed to open file" << endl;
          
   	while( getline(in,line) )
    {
        pos = line.find(','); 
                                  
        row=row +1;
        string loc = line.substr(0,pos);//start from position 0 until the value of pos (till comma):
                                        //to get first part of the line string example field=0.17960719
        string time = line.substr(pos+1);// get second value after comma
		time = time.substr(0, time.find(','));

        st2int=string_to_int(loc);// == location
		if(st2int >=0)
			LocY.push_back(st2int);

		// added on 11 feb
		/*if (st2int==0){
            SpecialE.push_back(st2int);
            special++;
			x.push_back(-1);
			goto loop;
		}*/

		st2int=string_to_int(time);
		TimeX.push_back(st2int);

	  }

    cout <<"no. of rows in csv file=" <<row << endl;
  
}
void DisplayVectorContents(vector <int> d )
{
      //cout << "\nVector contains: "<< endl;
	unsigned int i;
	for(i=0; i<d.size(); ++i)
        cout << "v["<<i << "]= "<<d[i]<<endl;
}


int main()
{
	
	//vector<Cluster *> clusters;//create vector of objects ?
	
	//should be vector<vector<cluster>> clusters;??
	

	vector <int> TimeX;//Time values
         vector <int> LocY;//Location values
	vector <int> SpecialE;//special event
	
	//8 locations value 
	vector <int> m1;
	vector <int> m2;
	vector <int> m3;
	vector <int> m4;
	vector <int> m5;
	vector <int> m6;
	vector <int> m7;
	vector <int> m8;
	
	ifstream in("KAllDataInt.csv");
    	
	Data_into_vectors(in,TimeX,LocY);

///////////////////////////////////////////////////////////////
	//check location types and then store each TimeX values(time) for each LocY
	// make it as function called population: pass TimeX ,LocY and new vectors ??
	//vector<vector<int>> m;
	//m[j].push_bak

	//void partition( tmix,locy,vector<vector<int>> &m){m1 to m8 by refernce
	for(unsigned int i=0; i<TimeX.size();i++)
	{
		if (LocY[i]==10000)//katesoffice
		{
			if (TimeX[i]>0)
			m1.push_back(TimeX[i]);
		}

		else if (LocY[i]==20000)//mailroom137
		{
			if (TimeX[i]>0)
			m2.push_back(TimeX[i]);		
		}

		else if(LocY[i]==30000)//MainEntrance
		{
			if (TimeX[i]!=-1)
			m3.push_back(TimeX[i]);
		}

		else if(LocY[i]==40000)//Room136
		{
			if (TimeX[i]!=-1)
			m4.push_back(TimeX[i]);
		}

		else if (LocY[i]==50000)//PhDOffices252
		{
			if (TimeX[i]!=-1)
			m5.push_back(TimeX[i]);
		}

		else if (LocY[i]==60000)//Adil212
		{
			if (TimeX[i]!=-1)
			m6.push_back(TimeX[i]);
		}

		else if (LocY[i]==70000)//SchoolOffice132
		{
			if (TimeX[i]!=-1)
			m7.push_back(TimeX[i]);
		}

		else if (LocY[i]==80000)//CNTROffices253
		{
			if (TimeX[i]!=-1)
			m8.push_back(TimeX[i]);
		}
		
	}
//////////////////////////////////////////////////////////////////
	int p;
	int r=1,k1=0,f=1,c;
	unsigned int i1=0;//because .size return unsigned int. then no warning
	unsigned int j=0,k=0;

	bool value;
	//Cluster c;
	vector <Cluster *> Cs; //**** creates Cs as a vector(dynamic array) of Cluster instance pointers
	
	vector<vector<int> *> clusters1;//vector of vector of pointers
	//vector<int> *cluster1=new vector<int>;// pointer to make new vector. this is like int *a=new int[];
	 	
	for( i1=0;i1<m1.size();i1++)
	{
	
		Cs.push_back(c);
		value=Cs[i1]->add_member(m1[i1]);
		if (value ==true)
		//get next point // go to loop m1[i1+1]
		break;
		else 
		{	
		//create new vector to get the point in it
		vector<int> *cluster1=new vector<int>;// this step should be outside for loop???
		clusters1.push_back(cluster1);//point
		}
		
return 0;		
 }

every time I have to pass value from vector m1 to the add_member function to check whether this value will be store in the same vector or a new vector until I get n vector (cluster).

Many Thanks

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by banoo2010
0

Could anybody explain how can i used vector of pointers to the Cluster class to divide a vector (m1)into unknown number of vectors? how can I create new vector each time I need and save the required data from the original vector(m1)

vector <Cluster *> Clusters; 
Cluster *C=new Cluster(); //pointer to new object


for( i1=0;i1<m1.size();i1++)
{

    value=C->add_member(m1[i1]);
    Clusters.push_back(C);          
    if (value ==false)
    {   
        //create new vector to get the point in it
        /*C->add_member(m1[i1]);*/
        Clusters.push_back(new Cluster());//point
        //Clusters.push_back(&m1[i1]);


    }

}

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.