class Data { string name; string ID; public: Data(){cout<<"Data"<<endl;} }; class info{ string name; string ID; public: info(){cout<<"info"<<endl;} }; Two classes defined above. Now I want to create a vector of type std::vector<Data obj1, info obj2> container; further I want to use the vector features using the vector container. How i can create a vector container having two class objects ? please help me.

Member Avatar
Member Avatar
+0 forum 2

For my own use, I'd like to develop a small Vector and a Matrix class, independent of some big libraries that exist out there. My question is: Should Vector and Matrix stay two independent entities or should I derive a Vctor frm a Matrix or a Matrix from a Vector? Any response is as always greatly appreciated. :)

Member Avatar
Member Avatar
+0 forum 9

vector<vector<double>> v2d(2, vector<double>(3,0.0)); How later on reset all elements back to 0.0?. How to clear vec entirely, free memory?. Are there any performance degradation in using vector arrays verus conventionl arrays using new or malloc?.. What can be done to minize such degradation if any?.

Member Avatar
Member Avatar
+0 forum 2

Can you tell me what's wrong with it because it's not running on visual studio and I need it right now??? #include <iostream> #include<vector> #include<algorithm> #include<limits> #include<fstream> #include<string> using namespace std; class PayRoll { private: vector<int> salaries; public: PayRoll(); bool searchSalary(int salary); void generateStats(); void printSalaries(); }; PayRoll::PayRoll() { ifstream in("newdata.txt"); if(!in.good()) { cout<<"Sorry Data File is not Found"<<endl; exit(1); } else { string str; while(!in.eof()) { in>>str; int temp = atoi(str.c_str()); salaries.push_back(temp); } } } bool PayRoll::searchSalary(int salary) { return (find(salaries.begin(),salaries.end(),salary) != salaries.end()); } void PayRoll::generateStats() { int minimumSalary = numeric_limits<int>::max(); int maximumSalary = numeric_limits<int>::min(); for(vector<int>::const_iterator iter = salaries.begin();iter<salaries.end();iter++) …

Member Avatar
Member Avatar
+0 forum 2

Guys, In below code after reading string values from vector , I can have output like("VarID","flag","count") for(int i=0; i < v.size(); i++){ cout<<v[i]<<endl; } All are (varID,flag and count)are string literal , now i need to populate the structre with this string literal. where VarID = int,flag=bool, count=Int. Can anybody suggest regarding this how this converson will take place as i am new to STL.

Member Avatar
Member Avatar
+0 forum 5

Hello everybody. I'm trying to write program with vectors and iterators. Coordinates X and Y are entered from keyboard (as structure) and placed in vector of points. It's necessary to display coordinates with maximal X and Y. This code was compiled but not display maximal X. What should I change? #include <iostream.h> #include <conio.h> #include <vector.h> #include <math.h> struct points { float x, y; }; int main() { using namespace std; int i; int max; vector<points>pointvect; //vector declaration points point; cout << "Enter a dimension of vectors:\n"; cin >> i; for (int k = 0; k <= i; k++) { …

Member Avatar
Member Avatar
+0 forum 3

Hello everybody! I'm trying to display the result of difference of two-dimensional vectors using friend overloading. Builder shows the following error after compilation (line 40). What should I change for in my code in order to find the problem? Thanks in advance. **[C++ Error] 05_Part.cpp(40): E2034 Cannot convert 'double' to 'Vector2D'** #include <iostream.h> #include <conio.h> class Vector2D { double x, y; friend Vector2D operator- ( const Vector2D &, const Vector2D & ); public: friend ostream & operator << (ostream & os, const Vector2D & cv) { os << cv.x << ',' << cv.y; return os; } }; Vector2D operator- ( …

Member Avatar
Member Avatar
+0 forum 4

I have declared, map<int, vector<int>> abcmap; map<int,vector<int>>:: iterator itmap; vector<int>SPLINKS; SPLINKS contains: 1 3 4 abcmap contains; 1=> 1 2=> 2 6 3=> 1 2 4 I want to iterate both SPLINKS and abcmap to find and erase elements that do not match in both containers; ie after execution; 2=>2 6 //not matched with either 1 or 3 or 4 I write something like; for(itmap = abcmap.begin(); itmap != abcmap.end(); ++itmap) { cout << endl << itmap->first <<" => "; for(size_t n=0; n<(*itmap).second.size(); n++) { for(itspl=SPLINKS.begin(); itspl!=SPLINKS.end(); ++itspl) { if((*itspl)==itmap->second[n]) { abcmap.erase(itmap); } } } } But I get debug …

Member Avatar
Member Avatar
+0 forum 2

I have delared two multimaps such as; multimap<int,vector<int>> allpathsmap; multimap<int,vector<int>>:: iterator itapm; typedef pair<int,vector<int>> pairapm; multimap<int,vector<int>> graphtextmap; multimap<int,vector<int>>:: iterator itgtm; typedef pair<int,vector<int>> pairgtm; And I wrote the following code to traverse the values of multimaps to print the key; for(itgtm=graphtextmap.begin(); itgtm!=graphtextmap.end(); ++itgtm) { for(itapm=allpathsmap.begin(); itapm!=allpathsmap.end(); ++itapm) { cout << endl << itapm->first <<" => "; for(size_t m=0 ; m<(*itgtm).second.size(); ++m ) { for(size_t n=0; n<(*itapm).second.size(); ++n) { if((itgtm->second[n]==itapm->second[m])&&(itgtm->second[n+1]==itapm->second[m+1])) { cout<<itgtm->first; } } } } } From the code I get debug assertion failed as "vector subscript out of range". Can ony one please fix the problem?

Member Avatar
Member Avatar
+0 forum 1

I'm working on a bit vector class that I'm using in a current project. Basicly BitVector is a container class for storing and manipulating data a bit level. I have two member fuctions for "setting" and "getting" bits at a given index. // Returns the bit at the specified index. unsigned char BitVector::at(int index) { if (index < 0 || index >= size) throw; // ("Index outside the range of vector: " + index); int byteValue = vector[index >> 3] & 0xff; return (unsigned char)((byteValue >> (7 - (index & 0x7))) & 1); } // Replaces the bit at index …

Member Avatar
Member Avatar
+0 forum 4

Is there a way to sort a vector with a class that has an int variable? I want to have my vector sorted and printed in console. I want to be able to sort the vectors. Say i create a bunch of vectors here on user input `vector<PCB> Disks2[DiskDevices];` Basically if i choose `vector<PCB> Disks2[1];` itll sort 1 if i choose `vector<PCB> Disks2[2];` itll sort 2 based on the Cylinder in the class. And i have this class class PCB { public: void setPID (int a) { PID = a; } int retrievePID() { return PID; } void setFilename (string …

Member Avatar
Member Avatar
+0 forum 3

Hello, I have the following problem: Entering coordinates of points on a plain with keyboard (as rows x,y). Analysing this coordinates. Replacing coordinates in vector (point - structure data type). Then calculating distance from the first point to all another points. I've written the code of this problem using arrays -> formula r=sqrt( (x[i]-x[1])^2+(y[i]-y[1])^2) ), (coordinate plain). #include <stdio.h> #include <iostream.h> #include <math.h> main() { float x[100], y[100]; int i, n; float r; cout << "Enter dimension of 1D massives (vectors of X & Y coordinates)\n"; cin >> n; cout << "Enter X coordinates of points for the 1st 1D …

Member Avatar
Member Avatar
+0 forum 5

Hi, I created a structure Vector and implement some functions to make the new defined type (vector) dynamically allocated and resized (inspired from the C++ implementation of the dynamic arrays : vector). I need to assign a structure to every vector element but I am not sure that I am doing it right: here is the structure that I ve defined: ************************************************** ********** typedef struct { void** mem; // to make this parametrizable I want the void* to point to a Structure (referenced with * a pointer) unsigned long elems; unsigned long elemsize; //element size unsigned long capelems; //capacity of …

Member Avatar
Member Avatar
+0 forum 6

vector <int> a; //first vector vector <int> b; //second vector vector <int>::iterator i; //iterator for first vector vector <int>::iterator i1; //iterator for second vector //reading of first vector int temp; while(cin>>temp) { a.push_back(temp); } i=a.begin(); while(i!=a.end()) { cout<<*i<<" "; i++; } //end of reading first vector //reading of second vector int temp2; while(cin>>temp2) { b.push_back(temp2); } i1=b.begin(); while(i1!=b.end()) { cout<<*i1<<" "; i1++; } // end of second vector i can read elements for 1st vector .however program doesnot go for second vector.

Member Avatar
Member Avatar
+0 forum 3

for(i=a.begin();i!=a.end();i++) { cin>>*i; } for(i=a.begin();i!=a.end();i++) { cout<<*i; } above code works fine but when i use while loop to display vector , program crash. while(i!=a.end();) { cout<<*i; i++; }

Member Avatar
Member Avatar
+0 forum 1

I rote this code in my program JSONNode::const_iterator iter = root.begin(); for (; iter!=root.end(); ++iter) { const JSONNode& arrayNode = *iter; std::string type = arrayNode["type"].as_string(); if(type == "node") { std::string id = arrayNode["id"].as_string(); double lat = arrayNode["lat"].as_float(); double lon = arrayNode["lon"].as_float(); Node node; node.SetId(id); node.SetLatitude(lat); node.SetLongitude(lon); nodesMap.insert( std::pair<std::string, Node>(id, node) ); } else if(type == "way") { std::string wayId = arrayNode["id"].as_string(); wayNode.SetId(wayId); std::vector<Node> collection; const JSONNode& wayNodes = arrayNode["nodes"]; const JSONNode& nodes = wayNodes.as_array(); JSONNode::const_iterator WayIter = nodes.begin(); for (; WayIter!=nodes.end(); ++WayIter) { const JSONNode& arrayNode = *WayIter; std::string id = arrayNode.as_string(); if(nodesMap.find(id) != nodesMap.end()) { collection.push_back(nodesMap.find(id)->second); nodesMap.erase(id); } } …

Member Avatar
Member Avatar
+0 forum 1

Hi everyone and thanks in advance for the help. I have the following class that throws an error on compiling where i declase a vector of pointers I have an Engine class that I want to host multiple scenes objects stored in the vector sceneList and then pass which ever I want to render to a SceneBase pointer #pragma once #include "Matrix3D.h" #include "SceneBase.h" #include <vector> class Engine { public: static float deltaTime; Engine(); ~Engine(); void Init(int argc, char* argv[]); void Run(); /* Forward declared GLUT callbacks registered by main. */ static void Display(); static void Update(); static void Keyboard(unsigned …

Member Avatar
Member Avatar
+0 forum 3

Okay so I have a class `Student`, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one! //Student.h #ifndef __Grade_calculator__Student__ #define __Grade_calculator__Student__ #include <iostream> #include <vector> using namespace std; class Student { private: string name; int score; int class_count; int test_count; vector <Student> students; vector <int> Scores; public: Student(); Student(string n,vector<int> &s); ~Student(); int getScore(){return score;} void …

Member Avatar
Member Avatar
+0 forum 8

my code: class A{ Vector<String> s = new Vector<String>(); } public class Vec { /** * @param args the command line arguments */ public static void main(String[] args) { A a = new A(); a.s.add("String1"); a.s.add("String2"); a.s.add("String3"); a.s.add("String4"); a.s.add("String5"); Vector<A> vecA = new Vector<A>(); vecA.add(a); System.out.println("Size of vector is "+vecA.size()); a = new A(); a.s.add("String6"); a.s.add("String7"); a.s.add("String8"); a.s.add("String9"); a.s.add("String10"); vecA.add(a); System.out.println("Size of vector is "+vecA.size()); for(int i=0;i<vecA.size();i++) { System.out.println(vecA.elementAt(i).s.elementAt(0)); System.out.println(vecA.elementAt(i).s.elementAt(1)); System.out.println(vecA.elementAt(i).s.elementAt(2)); System.out.println(vecA.elementAt(i).s.elementAt(3)); System.out.println(vecA.elementAt(i).s.elementAt(4)); } boolean b = vecA.contains("String1"); System.out.println(b); if(vecA.contains("String1")){ System.out.println("true"); }else{ System.out.println("false"); } } } my problem is: how to search for particular string in vecA ? i am …

Member Avatar
Member Avatar
+0 forum 2

Okay so I have an `inventory` class that accesses a static vector from my base class `MainShop`. I also have a `SwordShop` class which inherits from `MainShop`. (Both Inventory and SwordShop are derived classes). //SwordShop class void SwordShop::Shop { //I have a setter function where it takes an integer and a string and adds its to my hashmap. //Also have a getter function which has a for loop displaying my items in my map this->setWeaponSoldier(1, "1) Meito Ichimonji\n +4 Damage\n 150Gold"); this->setWeaponSoldier(2, "2) Shusui\n +10 Damage\n 230Gold"); this->setWeaponSoldier(3, "3) Elixir\n +16 Damage\n 300Gold"); this->setWeaponSoldier(4, "4) Blade of scars\n +24 Damage\n …

Member Avatar
Member Avatar
+0 forum 8

class A{ Vector<String> s = new Vector<String>(); } public class Vec { public static void main(String[] args) { A a = new A(); a.s.add("String1"); a.s.add("String2"); a.s.add("String3"); a.s.add("String4"); a.s.add("String5"); Vector<A> vecA = new Vector<A>(); vecA.add(a); System.out.println("Size of vector is "+vecA.size()); // here i am getting size one a.s.clear(); a.s.add("String6"); a.s.add("String7"); a.s.add("String8"); a.s.add("String9"); a.s.add("String10"); vecA.add(a); System.out.println("Size of vector is "+vecA.size()); // here i am getting size 2 for(int i=0;i<vecA.size();i++) { System.out.println(vecA.get(i).s.get(0)); System.out.println(vecA.get(i).s.get(1)); System.out.println(vecA.get(i).s.get(2)); System.out.println(vecA.get(i).s.get(3)); System.out.println(vecA.get(i).s.get(4)); } } } output : size of vector is 1 size of vector is 2 String6 String7 String8 String9 String10 String6 String7 String8 String9 String10 Here i …

Member Avatar
Member Avatar
+0 forum 3

// Expect to print values of arr[0] to arr[4] #include <iostream> #include <vector> using namespace std; int main() { vector<int *> ptrList; int arr[] = { 0, 10, 20, 30, 40 }; int * ptrArr = arr; for( int i=0; i< 5; i++ ) { ptrList.push_back( ptrArr+i ); } vector<int *>::iterator itr = ptrList.begin(); for( int i=1; i<= 5; i++ ) { cout << **itr-1 << endl; itr++; } return 0; } somebody help me please...dunno where i went wrong :(

Member Avatar
Member Avatar
+0 forum 7

Hi, I'm newbie with C++ and I found an example which I need http://ideone.com/Eikbr4 But I've a question, how do I seperate this piece of code into .h and .cpp.. I tried to do something similar on my own but I'm having struggling with it after multiple try.. I've read many sort article on the but it's just not help.. missionplan.h class MissionPlan { private: int sizeofarray; int sizeofarray2; int xcordi; int ycordi; static float civnum; public: MissionPlan(); MissionPlan(int, int, float); int getx(); int gety(); float getciv(); void stats(); void storedata(int, int, float); void test(); void displayall(); void compute(); void …

Member Avatar
Member Avatar
+0 forum 1

How to sort vector<int> v{3,2,7,6,5,8,1,2,3,4,5}; in decreasing order using function object. I did using lambda expression sort(v.begin(),v.end(),[](int a , int b){return a > b;}); But How we do using fn object

Member Avatar
Member Avatar
+0 forum 1

given int a[]= {3,2,7,6,8,1,4,5} vector<int> v(a,a+8) use the stl copy algorithm to display the last 5 integers in the vector to cout with each integer on a seperate line. copy takes three argument. third argument is output container. In this case it is v.but v already have 8 elemenst. How to solve this pb?

Member Avatar
Member Avatar
+0 forum 1

I need a little help to store two strings into a vector and reversing the order of each string. i.e. 123456789 needs to be 987654321 12345 needs to be 000054321 reason for that is I need to add those two numbers together.

Member Avatar
Member Avatar
+0 forum 7

I have to find the permutation of vector of integers. I need to implement and interleave function vector<vector<int> > interleave(int x , const vector<int> & v); Using interleave we can implement a permute function. vector<vector<int> > permute(size_t n); that returns a vector containing of the first n positive nos. I donot how to start. Need help

Member Avatar
Member Avatar
+0 forum 1

Hi, Imagine a vector of structures with each structure containing various variables. struct Data { double x double y double z } vector<Data> Dataset vector<Data>::iterator it //input some data from a file into DataSet How can I iterate over this vector, comparing say, x at the nth row to the x at the (n-1th) row and the x at the (n-2th) row, beginning at n (where n is Dataset.begin() + 2) and moving one n forward at a time? Thanks, TR

Member Avatar
Member Avatar
+0 forum 8

This is an assingment i've been working on but since this is a merge between two previous developments i need a little help solving my issue here that would be highly appreciated. In a nutshell what this program needs to do is recieve a .txt file create a vector holding all this values and apply them some sorting algorithms. Here's the code i wrote so far but as i said before what i think i need is a function to convert the vector values to an int array but i'm stuck there, so any help is appreciated. Main.cpp #include <iostream> …

Member Avatar
Member Avatar
+0 forum 3

Hello everyone! I would like your help regarding this: I have a tuple vector which i must iterate through, and accessing the vector data to decide wether to erase or not the current vector element. My code : typedef tuple <int, CString, int, int> pc_data; vector <pc_data> pc_vec; //Assume that pc_vec is filled with elements somewhere here... //This code is not working. //Exception : vector subject out of range because of the size dynamically changing i suppose for (int i=0; i<num_of_pcs; i++) { if(std::get<2>(pc_vec[i])==0) pc_vec.erase(pc_vec.begin()+i); } //second try //No exception here but the dynamic size of the vector again won't …

Member Avatar
Member Avatar
+0 forum 6

The End.