Hàm map C++

The map::find() is a built-in function in C++ STL that returns an iterator or a constant iterator that refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end()

Show


    Syntax: 

    iterator=map_name.find(key)
            or 
    constant iterator=map_name.find(key)

    Parameters: The function accepts one mandatory parameter key, which specifies the key to be searched in the map container. 

    Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end(). 

    The

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 method creates a new array populated with the results of calling a provided function on every element in the calling array.

    // Arrow function
    map((element) => { /* … */ })
    map((element, index) => { /* … */ })
    map((element, index, array) => { /* … */ })
    
    // Callback function
    map(callbackFn)
    map(callbackFn, thisArg)
    
    // Inline callback function
    map(function (element) { /* … */ })
    map(function (element, index) { /* … */ })
    map(function (element, index, array) { /* … */ })
    map(function (element, index, array) { /* … */ }, thisArg)
    

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3

    A function to execute for each element in the array. Its return value is added as a single element in the new array.

    The function is called with the following arguments:

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    4

    The current element being processed in the array.

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    5

    The index of the current element being processed in the array.

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    6

    The array

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 was called upon.

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    8 Optional

    A value to use as

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    9 when executing
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3. See .

    A new array with each element being the result of the callback function.

    The

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 method is an . It calls a provided
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3 function once for each element in an array and constructs a new array from the results.

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3 is invoked only for array indexes which have assigned values. It is not invoked for empty slots in .

    The

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 method is a . It does not alter
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    9. However, the function provided as
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3 can mutate the array. Note, however, that the length of the array is saved before the first invocation of
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    3. Therefore:

    • const numbers = [1, 4, 9];
      const roots = numbers.map((num) => Math.sqrt(num));
      
      // roots is now     [1, 2, 3]
      // numbers is still [1, 4, 9]
      
      3 will not visit any elements added beyond the array's initial length when the call to
      const numbers = [1, 4, 9];
      const roots = numbers.map((num) => Math.sqrt(num));
      
      // roots is now     [1, 2, 3]
      // numbers is still [1, 4, 9]
      
      2 began.
    • Changes to already-visited indexes do not cause
      const numbers = [1, 4, 9];
      const roots = numbers.map((num) => Math.sqrt(num));
      
      // roots is now     [1, 2, 3]
      // numbers is still [1, 4, 9]
      
      3 to be invoked on them again.
    • If an existing, yet-unvisited element of the array is changed by
      const numbers = [1, 4, 9];
      const roots = numbers.map((num) => Math.sqrt(num));
      
      // roots is now     [1, 2, 3]
      // numbers is still [1, 4, 9]
      
      3, its value passed to the
      const numbers = [1, 4, 9];
      const roots = numbers.map((num) => Math.sqrt(num));
      
      // roots is now     [1, 2, 3]
      // numbers is still [1, 4, 9]
      
      3 will be the value at the time that element gets visited. Deleted elements are not visited.

    Warning: Concurrent modifications of the kind described above frequently lead to hard-to-understand code and are generally to be avoided (except in special cases).

    The

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 method is . It only expects the
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    9 value to have a
    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    5 property and integer-keyed properties.

    Since

    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    6 builds a new array, calling it without using the returned array is an anti-pattern; use
    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    7 or
    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    8 instead.

    The following code takes an array of numbers and creates a new array containing the square roots of the numbers in the first array.

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    

    The following code takes an array of objects and creates a new array containing the newly reformatted objects.

    const kvArray = [
      { key: 1, value: 10 },
      { key: 2, value: 20 },
      { key: 3, value: 30 },
    ];
    
    const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));
    
    console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
    console.log(kvArray);
    // [
    //   { key: 1, value: 10 },
    //   { key: 2, value: 20 },
    //   { key: 3, value: 30 }
    // ]
    

    The following code shows how

    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    6 works when a function requiring one argument is used with it. The argument will automatically be assigned from each element of the array as
    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    6 loops through the original array.

    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    

    The

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2 method reads the
    const numbers = [1, 4, 9];
    const doubles = numbers.map((num) => num * 2);
    
    console.log(doubles); // [2, 8, 18]
    console.log(numbers); // [1, 4, 9]
    
    5 property of
    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    9 and then accesses each integer index.

    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    

    This example shows how to iterate through a collection of objects collected by

    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    
    4. This is because
    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    
    4 returns a
    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    
    6 (which is a collection of objects).

    In this case, we return all the selected

    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    
    7s' values on the screen:

    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    

    An easier way would be the

    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
    };
    console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    // [ 4, 9, 16 ]
    
    8 method.

    A sparse array remains sparse after

    const numbers = [1, 4, 9];
    const roots = numbers.map((num) => Math.sqrt(num));
    
    // roots is now     [1, 2, 3]
    // numbers is still [1, 4, 9]
    
    2. The indices of empty slots are still empty in the returned array, and the callback function won't be called on them.

    console.log(
      [1, , 3].map((x, index) => {
        console.log(`Visit ${index}`);
        return x * 2;
      }),
    );
    // Visit 0
    // Visit 2
    // [2, empty, 6]
    

    (inspired by this blog post)

    It is common to use the callback with one argument (the element being traversed). Certain functions are also commonly used with one argument, even though they take additional optional arguments. These habits may lead to confusing behaviors.

    Consider:

    ["1", "2", "3"].map(parseInt);
    

    While one might expect

    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    0, the actual result is
    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    1.

    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    2 is often used with one argument, but takes two. The first is an expression and the second is the radix to the callback function,
    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    3 passes 3 arguments:

    • the element
    • the index
    • the array

    The third argument is ignored by

    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    2—but not the second one! This is the source of possible confusion.

    Here is a concise example of the iteration steps:

    // parseInt(string, radix) -> map(parseInt(value, index))
    /* first iteration  (index is 0): */ parseInt("1", 0); // 1
    /* second iteration (index is 1): */ parseInt("2", 1); // NaN
    /* third iteration  (index is 2): */ parseInt("3", 2); // NaN
    

    Then let's talk about solutions.

    const returnInt = (element) => parseInt(element, 10);
    
    ["1", "2", "3"].map(returnInt); // [1, 2, 3]
    // Actual result is an array of numbers (as expected)
    
    // Same as above, but using the concise arrow function syntax
    ["1", "2", "3"].map((str) => parseInt(str)); // [1, 2, 3]
    
    // A simpler way to achieve the above, while avoiding the "gotcha":
    ["1", "2", "3"].map(Number); // [1, 2, 3]
    
    // But unlike parseInt(), Number() will also return a float or (resolved) exponential notation:
    ["1.1", "2.2e2", "3e300"].map(Number); // [1.1, 220, 3e+300]
    
    // For comparison, if we use parseInt() on the array above:
    ["1.1", "2.2e2", "3e300"].map((str) => parseInt(str)); // [1, 2, 3]
    

    One alternative output of the map method being called with

    const elems = document.querySelectorAll("select option:checked");
    const values = Array.prototype.map.call(elems, ({ value }) => value);
    
    2 as a parameter runs as follows: