vector in c++ class
C++
#include <iostream>
#include <vector>
class Object
{
public:
Object()
{}
~Object()
{}
void AddInt(int num)
{
m_VectorOfInts.push_back(num);
}
std::vector<int> GetCopyOfVector()
{
return m_VectorOfInts;
}
void DisplayVectorContents()
{
for( unsigned int i = 0; i < m_VectorOfInts.size(); i++ )
{
std::cout << "Element[" << i << "] = " << m_VectorOfInts[i] << std::endl;
}
std::cout << std::endl;
}
private:
std::vector<int> m_VectorOfInts;
};
int main()
{
// Create our class an add a few ints
Object obj;
obj.AddInt(32);
obj.AddInt(56);
obj.AddInt(21);
// Display the vector contents so far
obj.DisplayVectorContents();
// Creates a copy of the classes container you can only really view whats in
// the classes vector container. What ever changes you make here wont effect the class.
std::vector<int> container1 = obj.GetCopyOfVector();
// These elements wont be added as it's a copy of the container
container1.push_back(342);
container1.push_back(64);
container1.push_back(123);
// Display the classes container to see show nothing was added.
obj.DisplayVectorContents();
return 0;
}
Also in C++:
- Title
- memset c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- date to string c++
- Category
- C++
- Title
- length of 2d array c++
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++
- Title
- how to convert number to string
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- set in c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- for loop in c++ hackerrank solution
- Category
- C++
- Title
- correct sequence of compilation process in c++
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- how to add a number after each number in an array with a for loop in C++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- factorial in c++
- Category
- C++
- Title
- dfenwick tree code c++
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- how to make loop in c++
- Category
- C++
- Title
- mao two drivers c++
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- c++ create button
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- c++ read_ascii
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- find vector in c++
- Category
- C++
- Title
- add two numbers in c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- timer in c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- how to cin multiple lines of strings c++
- Category
- C++
- Title
- calculate sum in c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- balanced brackets hackerrank solution in cpp
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- how to extract substring from string in c++
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- accumulate c++ stl
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- c++ cast char to string
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- formal parameter c++
- Category
- C++
- Title
- how to reverse a vector
- Category
- C++
- Title
- use of strstr in c++
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- binary exponentiation
- Category
- C++
- Title
- c++ bsod
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- variable sized arrays hackerrank solution in c++
- Category
- C++
- Title
- c++ operator overloading not equal
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++