-2

i am new to C++ and I was wondering what the below code does. I tried googling but could not find the answer. Thanks

vector<int> dist(n, INT_MAX);
Jason
  • 36,170
  • 5
  • 26
  • 60
  • 2
    Creates a vector of size `n` and initialises each element to `INT_MAX`. – john Jul 23 '22 at 11:59
  • 3
    `std::vector` has a lot of different constructors, just browse cppreference: https://en.cppreference.com/w/cpp/container/vector/vector – Jack Jul 23 '22 at 11:59
  • [check the list of constructors for `std::vector`](https://en.cppreference.com/w/cpp/container/vector/vector), it's the 3rd type in the list. You should [read a good book](https://stackoverflow.com/q/388242/995714) instead of seeing random code snippets or try random programming problems, that'll make your future questions be closed very quickly – phuclv Jul 23 '22 at 11:59
  • 3
    You definitely should find reference material you are comfortable with so you can look this stuff up for yourself. – john Jul 23 '22 at 12:00
  • if `x` and `INT_MAX` are type here, it can also be a function declaration. – apple apple Jul 23 '22 at 12:22
  • @ferocioussprouts122 Refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), this is explained in any beginner level C++ book. – Jason Jul 23 '22 at 12:32

2 Answers2

1

The line of code serves to declare a variable called dist, and to initialise it is a std::vector object.

The <int> part of the vector initialisation is called a template argument. The std::vector class is templated, which means it can store any arbitrary data-type, which is why the type has to be declared within angled brackets <>.

As mentioned in the comments, n and INT_MAX are values being passed to one of the constructors of the std::vector. It is only one of the constructors, since there are various different overloads of the constructor (check out the documentation in the comments to your question to read about the different available constructors).

Function (and also method) overloading is when you declare various functions with the same name, but that differ in the number and/or types of parameters that can be passed to them (the return type can also vary).

1

Vector is a container C++ has already prepared.

I guess u should read the manual of STL before u use the class in it. And next is the answer;

Vector just like a string,but it has built many useful functions in it which make programers more inclined to use.

vector<int> dist(n, INT_MAX); the template is vector<type> name().

(n, INT_MAX) is initialization statement, it will put n INT_MAXs to the vector.

INT_MAX means largest number of type int;

If it helps you, please cast me a vote.

fabian
  • 80,457
  • 12
  • 86
  • 114
付浩翔
  • 11
  • 1
  • What's the point of defining a container in terms of another container, both of which are possibly unknown to the OP? Furthermore: You don't consider `std::string` to provide useful functions? The reason for using `std::vector` is that `std::string` is designed specifically to work with character sequences and `std::vector` is designed as as container for arbitrary element types. – fabian Jul 23 '22 at 12:34