0
How to print the statement?
We have a vector: vector<int> v={37, 4, 5, 100, 14, 15}; I would like to print a string statement result = [["M: 37", "B: 4", "C: 5"], ["M: 100", "B: 14", "C: 15"]]; Does anybody knows how to print string result in such form? Size of vector can be unknown. https://code.sololearn.com/cQn1M87rMy13/?ref=app
13 ответов
+ 2
TeaserCode ...
I wrote this earlier, but since you haven't shared a code bit link, I'm not even sure this was what you plan to do, at all ...
https://code.sololearn.com/ceK9VdPpF0n2/?ref=app
+ 1
Use printf, which accepts strings and interpolated values in arbitrary formats
+ 1
In your output example, each row was formed from 3 elements (M, B, C) Apart from unknown/unpredictable size, what if the number of elements were not fully divisible by 3? would there be an incomplete row?
+ 1
It's easy to know the size of the 2d vector. It receives the items from the 1d vector, divided in 3 item inner vectors. So, just divide the 1d vector size by 3.
Also easy to form the 2d vector from the 1d vector. Just iterate the 1d vector items, and calculate the outer and inner 2d vector indexes, respectively, from the result and remainder of the 1d vector index by 3.
+ 1
TeaserCode
Are you into transforming the `int` vector into a 2D `string` vector, or just printing the `int` vector with some format? I was wondering what you meant by "string with subvectors"
Any code for a review and discuss further?
+ 1
TeaserCode Could you pls save this code in Code Playground and add a link to it in the question description? I'd like to see it running.
0
No, each of three parameters have always its value. But I don't know how to do 2d string vector from vector v. So 2d vector should have, for this example two rows with three columns. Then I would make a string with subvectors. Here is a problem with printing "M: ", "B1: " snd "C1: ".
When I declare 2d vector, do I have to know the size of it?
0
I try to print the content of the "int" vector into string format in such form as it said.
0
Can you write it with 2d string vector. If it is possible!
#include <iostream>
#include<vector>
#include<cstring>
using namespace std;
int main(int argc, char *argv[]) {
int m=0, n=200, b=7, c=9, ost1=2, ost2=1, i=2, B=1, C=1;
vector<string> v;
string M, B1, C1;
if(m > n) {
swap(m, n);
}
cout<<"m: "<<m<<" n: "<<n<<endl;
while(C < n && i < n) {
i=ost2+c*C;
B=(i-ost1)/b;
for(B = 1; B < C; B++) {
B=(i-ost1)/b;
if(i-b*B==ost1 && i>=m && i<= n) {
v.push_back(to_string(i));
v.push_back(to_string(B));
v.push_back(to_string(C));
}
}
i++;
C++;
}
for(unsigned i=0; i<v.size(); i++)
cout<< v[i]<<" ";
return 0;
}
/*
howmuch(1, 100) => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"]]
howmuch(1000, 1100) => [["M: 1045", "B: 149", "C: 116"]] */
Here is my code. I hope you will know what does it make.
0
TeaserCode,
I don't understand what the code was calculating. Variables' names were not self explanatory enough to understand what they were either,
But I think you can achieve what you seek for by focusing on formatting the `string` you push into the `vector` ...
P.S. Attach a code bit link so we can refer line numbers, it's not possible in raw text replies.
0
TeaserCode From what I understood, you have one vector, include all strings in it, then output the whole vector.
A possible solution would be to write to the vector one row at a time, then output the row and reset the vector.
As Ipang noticed, the variable names are too obscure. I can't tell what they are. Try to rename them to refleft their meanings.
In fact, code clearness is key for you to understand your own code when you have to do later fixes or improvements.
0
Ok, thank you.
- 1
print(2 + 2)
print(5 + 4 - 3)