#include <iostream>
#include <string>
#include <vector>
#include <sstream>
class data {
public:
// Constructor
data() {
std::cout << "constructor called" << std::endl;
}
// Vector to store integers
std::vector<int> arr;
// String and vector of strings
std::string str;
std::vector<std::string> vs;
// Destructor
~data() {
std::cout << "destructor called" << std::endl;
}
};
int main() {
data a1;
int elem;
std::string stg;
// Input number of elements
std::cout << "Enter the number of integers: ";
std::cin >> elem;
// Input integers into the vector
std::cout << "Enter " << elem << " integers: " << std::endl;
for (int i = 0; i < elem; i++) {
int value; // Use a separate variable to capture input
std::cin >> value;
a1.arr.push_back(value);
}
// Display the integers
std::cout << "Integers in the array: " << std::endl;
for (auto &v : a1.arr) {
std::cout << v << std::endl;
}
// Ignore leftover newline
std::cin.ignore();
// Input strings into the vector
std::cout << "Enter " << elem << " strings: " << std::endl;
std::getline(std::cin, stg); // Read a full line of string input
a1.vs.push_back(stg);
// Display the strings
std::cout << "Strings in the array: " << std::endl;
for(const auto &line: a1.vs){
std::istringstream yes(line);
std::string word;
while(yes>>word){
std::cout<<word<<std::endl;
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: