new to programming so I apologize if this has been answered before...I have tried searching with almost zero luck. I am trying to create a number pattern like 1,2,2,3,3,3,4,4,4,4 etc; I have gotten to that point but I need to put it into a string vector so that if the user inputs 5, the string vector elements are 1,2,2,3,3,3,4,4,4,4,5,5,5,5,5
void print_vectorStr(vector<string> &vec, string sep = "")
{
cout << "\nThe vector string elements are: ";
for (auto elem : vec)
{
cout << elem << sep;
}
cout << endl;
}
void print_vector(vector<int> &vec, string sep = "")
{
cout << "\nThe vector integer elements are: ";
for (auto elem : vec)
{
cout << elem << sep;
}
cout << endl;
}
int main()
{
int n;
cout << "insert n \n";
cin >> n;
vector<int> vec(n);
vector<string> vecString;
for (int i = 1; i <= vec.size(); i )
{
for (int j = 1; j <= i; j )
{
cout << i << " ";
}
}
print_vector(vec, " ");
print_vectorStr(vecString, " ");
return 0;
}
CodePudding user response:
If you really need a vector of strings, and need to reproduce the output in a comma-separated pattern, you can do something simple like:
#include <iostream>
#include <vector>
#include <string>
int main () {
size_t n = 0; /* unsigned variable for n */
std::vector<std::string> pattern{}; /* vector of strings */
std::cout << "enter n: ";
if (!(std::cin >> n)) { /* validate EVERY user-input */
std::cerr << "error: invalid input.\n";
return 1;
}
for (size_t i = 0; i <= n; i ) { /* loop n times from 1 */
for (size_t j = 0; j < i; j ) { /* loop i times */
pattern.push_back(std::to_string(i)); /* add i i's to string */
}
}
/* loop over each string in pattern with index to control ',' output */
for (size_t i = 0; i < pattern.size(); i ) {
if (i) { /* if not first string */
std::cout.put(','); /* output ',' */
}
std::cout << pattern[i]; /* output string */
}
std::cout.put('\n'); /* tidy up with newline */
}
Note above you must validate EVERY user-input. Otherwise, what would happen if the user slipped and tapped 't'
instead of 5
? Also note, to control the ','
separator for output, you can either keep a counter and use a range-based for
loop, or, since you need a counter anyway, just use a normal conditioned for
loop.
Example Use/Output
$ ./bin/num_pattern
enter n: 2
1,2,2
$ ./bin/num_pattern
enter n: 5
1,2,2,3,3,3,4,4,4,4,5,5,5,5,5
or
$ ./bin/num_pattern
enter n: t
error: invalid input.
Look things over and let me know if you have further questions.
CodePudding user response:
I need to put it into a string vector
You can use std::vector::push_back
to add(push_back) more elements into a vector as shown below. Moreover, you can use std::to_string
to convert the int
to std::string
. Also note that there is no need to create a std::vector<int>
. You can just directly use push_back
to push/add elements into vecString
using to_string
as shown below:
#include <iostream>
#include<algorithm>
#include<string>
#include<vector>
void print_vectorStr(const std::vector<std::string> &vec, std::string sep = "")
{
std::cout << "\nThe vector string elements are: ";
for (auto elem : vec)
{
std::cout << elem << sep;
}
std::cout << std::endl;
}
int main()
{
int n;
std::cout << "insert n \n";
std::cin >> n;
std::vector<std::string> vecString;
for (int i = 1; i <= n; i )
{
for (int j = 1; j <= i; j )
{
vecString.push_back(std::to_string(i)); //add element into the vecString
}
}
print_vectorStr(vecString, " ");//prints the required pattern
}