-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput_Space_Separated.cpp
More file actions
36 lines (27 loc) · 1.54 KB
/
Input_Space_Separated.cpp
File metadata and controls
36 lines (27 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h> // Includes all standard C++ libraries
using namespace std; // Allows using standard functions without "std::" prefix
// Function to read space-separated numbers from input
vector<int> input_space_separated() {
vector<int> arr; // Create an empty vector (dynamic array) to store numbers
string line; // Create a string variable to hold the input line
getline(cin, line); // Read the entire line of input from user
stringstream ss(line); // Convert the string into a stringstream (makes it easy to extract data)
int num; // Temporary variable to store each number
while(ss >> num) { // Extract numbers one by one from the stringstream
// The >> operator automatically:
// 1. Skips any whitespace (spaces, tabs)
// 2. Reads the next number
// 3. Returns false when there are no more numbers
arr.push_back(num); // Add the number to our vector
}
return arr; // Return the vector containing all numbers
}
int main() {
// Call the function to get space-separated numbers
vector<int> arr = input_space_separated();
// Loop through the vector and print each number
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " "; // Print number followed by space
cout << endl; // Move to next line after printing all numbers
return 0; // Program ends successfully
}