0
Why output is different in both cases????please describe me....
/*to get output * ** *** **** ***** */ //I have written 2 codes in c++ Code 1. #include <iostream> #include <string> using namespace std ; int main(int argc, char *argv[]) { string star{"*****"}; for(unsigned i=1; i<=5; i++){ string str{star, i}; cout<<str<<endl; } return 0; } Code 2. #include <iostream> #include <string> using namespace std ; int main(int argc, char *argv[]) { //string star{"*****"}; for(unsigned i=1; i<=5; i++){ string str{"*****", i}; cout<<str<<endl; } return 0; } Output 1. **** *** ** * Output 2. * ** *** **** ***** As per expectation is output 2
8 Answers
+ 3
Ipang is right.
In the line,
`string str{star, i}`
Of rhe 1st code, the constructor being called is
```
basic_string(
const basic_string& other,
size_type pos,
const Allocator& alloc = Allocator() );
```
(number (3) in the link mentioned by Ipang)
If we read the description of this constructor:
"Constructs the string with a substring [pos, pos+count) of other. If count == npos, if count is not specified, or if the requested substring lasts past the end of the string, the resulting substring is [pos, other.size())"
This means that in that line, you are putting substring of `star` from `i` till the end into `str`. When i=1, string "****" (from pos 1 till last) will be created, when i=2, "***" (pos 2 till last) will be created, and so on
[continued in next answer]
+ 4
I'm thinking perhaps this happens due to the use of different std::string constructor overloads in the two snippets. Have a look at the overloads and find one that matches your constructor argument(s).
https://en.cppreference.com/w/cpp/string/basic_string/basic_string
+ 4
XXX's answer nailed it well, I was just guessing. Try to read XXX's answers, he had extended it with explanations of the constructor overloads' variety đ
+ 3
[continued from previous answer]
In the second code, the constructor being called is
```
basic_string(
const CharT* s,
size_type count,
const Allocator& alloc = Allocator() );
```
(number (4))
Description:
"Constructs the string with the first count characters of character string pointed to by s. s can contain null characters. The length of the string is count"
This means that the second argument you are passing (`i`) is for the length of the string. When i=1, the constructor thinks the length of the string passed is 1, and only creates a string from the 1st character, that is "*". When i=2, a string from the first 2 characters js created, that is "**", and so on
+ 3
Btw thanks for helping me.....đ
0
However if i use different style of array initialization in for loop as below..
string str{star, 0, i};
Or
string str{"*****", 0, i};
No matter whether i am using c++ string or c style literal inside {} ,it gives
gives expected output.......
I observed these things but don't aware about reasons behind these things
..
Please help me to clear these behind the scenes concepts...
0
I tried but there are so much unknowns to me ....