Same behaviour for size() vs .size() with string? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is there any difference in output behaviour between using size(x) instead of x.size(), where x is a string variable? Or it is just another alias?

The behavior of std::size is:
Returns the size of the given container c or array array.
1-2) Returns c.size(), converted to the return type if necessary.
...
So calling std::size(x) and x.size() has the same effect, where x is a std::string.
(This function can also accept an array, but that's not relevant when it comes to a std::string).

Related

what is npos ??? My problem is I do not understand why anyone would use it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is npos ??? I recently saw it in some source code and really want to know what it is used for and why it is used. An example would be nice also. Thanks(:
The documentation says :
npos is a static member constant value with the greatest possible
value for an element of type size_t
This value, when used as the value for a len (or sublen) parameter
in string's member functions, means "until the end of the string".
As a return value, it is usually used to indicate no matches.
It is defined internally as :
static const size_t npos = -1;
Just remember, it's used to indicate not found.

String comparison function (c++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am looking for a function to compare the two strings. A functional similar to strcmp in CString with the difference that takes two strands in the input.
You can use std::string::compare (it returns 0 if values are the same). Also be aware that in fact you can use strcmp in c++, but if you want modern c++ version i would go with std::string::compare.

I don't understand this vector declaration statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Why LOG and 0 are used in this statement?
LOG = 35;
vector<int> cnt(LOG, 0); //here cnt is a vector name
The statement is constructing a std::vector, so you should look at help documentation for the vector constructor.
In this case, LOG and 0 are being used specifically in the two-parameter override of the constructor.
LOG is used to specify the initial size of the vector;
0 is used to specify the initial value of the elements.
In other words, the expression declares a vector named cnt being of size 35 with all elements initialized to 0.

What is Default value of array on heap in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
When I print the default value of array on heap memory I'm getting random big numbers in code block. I know that the default value of array is 0 but I am getting random number .
The doc of the std::array constructor says it:
initializes the array following the rules of aggregate initialization (note that default initialization may result in indeterminate values for non-class T)

What is Predecessor of an element in an array? How do I find it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried to Google but in vain.
In C++ arrays are stored in contiguous memory.
This means that if you have an element x and you know that it's inside an array and that it's not the first element of the array, then then previous one is
*(&x - 1)
i.e. the element pointed by the address of x after we subtract 1 (note that this works because pointer arithmetic in C++ considers element size, so &x - 1 is not point to the byte before, but to the element before).