1

Wanted to know what does s = ""s does in the following code and how is it used!

generate(begin(v), end(v), [s = ""s, c = '`']() mutable { ++c; s += c; return s; });
#include <bits/stdc++.h>

using namespace std;


void use_generate_abc() {
  vector<string> v(5);
  generate(begin(v), end(v), [s = ""s, c = '`']() mutable { ++c; s += c; return s; });
  for(auto x: v)cout << x << " ";
}

int main(){
   std::ios_base::sync_with_stdio(false);
   std::cin.tie(NULL);
   use_generate_abc();
   return 0;
}
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
Chiranjeev
  • 29
  • 5
  • 5
    On an unrelated note, please [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Jun 08 '20 at 06:20
  • @Someprogrammerdude, I literally am tired of advising people that. They simply don't listen :( – kesarling Jun 08 '20 at 06:21
  • 3
    And not to mention usage of `using namespace std` – kesarling Jun 08 '20 at 06:24
  • 4
    And that `std::ios_base::sync_with_stdio(false);` isn't really needed. It's just something that's for some inexplicably reason happens to be popular on online "competition" sites. Sites which too many people use to learn how to program. And all they will learn is to program for such sites and nothing else. If you really want to learn C++ and programming, get [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take a couple of classes. – Some programmer dude Jun 08 '20 at 06:36
  • 1
    guess he is coming from leetcode/hackerrank :) – djacob Jun 08 '20 at 07:05
  • @d4rk4ng31 even if the person does not care about `bits/stdc++.h` it is still good to mention it for any other reader. Comments are not only for the OP but for anyone finding that question. – t.niese Jun 08 '20 at 07:13
  • bits/stdc++.h was included so that the main focus of the question remain on the use_generate_abc function rather than the libraries included! Btw thanks for the the answer @Someprogrammerdude , btw I don't really use printf and scanf so using std::ios_base::sync_with_stdio(false); works fine for me! – Chiranjeev Jun 09 '20 at 05:30
  • 1
    @Chiranjeev Just as long as you remember that those are bad habits (including [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). And unfortunately habits (good and bad) tend to stick. – Some programmer dude Jun 09 '20 at 07:51

1 Answers1

2

It's a user-defined literal. The s suffix turns it into a std::string.

For example, lets say you have

auto s = ""s;

Then it's equivalent to

auto s = std::string();

Or just

std::string s;

In other words, it creates an empty std::string.


In combination with the lambda capture it defines the variable s as an empty std::string object, and captures it for use inside the lambda.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • 2
    @ThomasSablik From the [user-defined literal reference](https://en.cppreference.com/w/cpp/language/user_literal): "All *ud-suffixes* introduced by a program must begin with the underscore character `_`. **The standard library *ud-suffixes* do not begin with underscores.**" (emphasis mine) – Some programmer dude Jun 08 '20 at 06:59