473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to use own objects as elements of STL vector

hi,,
actually i want to know how to use own objects as elements in STL vector.
we can easily use int, float , char etc. as the elements of vector, but how to use own created objects as the element of vector.....

SAURABH
May 17 '07 #1
4 1428
gpraghuram
1,275 Expert 1GB
Hi,
The mode of usage remains the same for created objects.
Check this
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. int x;
  4. };
  5. vector <A> AVect;
  6.  
Thanks
Raghuram
May 17 '07 #2
actually this code is not working..
when i create the obejct and then use the push_back() function, it gives some exception .
can you provide me the full code as how to use the push_back() function in this case to add object to vector.
May 17 '07 #3
gpraghuram
1,275 Expert 1GB
Hi,
Check this code...
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. class Test
  8. {
  9. private:
  10.     string m_name;
  11.     int m_age;
  12. public:
  13.     Test(string _name,int _age)
  14.     {
  15.         m_name.assign(_name);
  16.         m_age=_age;
  17.     }
  18.     void Print()
  19.     {
  20.         cout<<m_name<<" age is "<<m_age<<endl;
  21.     }
  22. };
  23.  
  24. Test CreateTest(char* _name,int _age)
  25. {
  26.     string memStr(_name);
  27.     Test retTest(_name,_age);
  28.     return retTest;
  29. }
  30.  
  31. int main()
  32. {
  33.     vector<Test> TVector;
  34.     Test t1 = CreateTest("A",10);
  35.     TVector.push_back(t1);
  36.     t1 = CreateTest("B",20);
  37.     TVector.push_back(t1);
  38.     t1 = CreateTest("C",30);
  39.     TVector.push_back(t1);
  40.  
  41.     vector<Test>::iterator TVIter;
  42.     for(TVIter=TVector.begin();TVIter!=TVector.end();TVIter++)
  43.     {
  44.         (*TVIter).Print();
  45.     }
  46.  
  47.  
  48. }
  49.  
Thanks
Raghuram
May 17 '07 #4
thanks Raghuram

I got idea..........
thanks a lot.......
May 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Bo Peng | last post by:
Dear List, It is not clear what the title means. :-) Here is the details: I need to manage a big bunch of small objects, like struct{ int a; int b; }obj;
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
25
by: Jack | last post by:
Hi, Is there a general solution for the following problem: I have an array of instances of class B. Class B is publicly derived from class A. Then I have a class named Buffer that generally...
3
by: orkonoid | last post by:
Hello, I am having trouble with a Polymorphism issue using container classes. I have a longwinded and shortwinded version of my question: Shortwinded version: How can I store base class...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.