📜  vector :: C++ STL中的Assign()

📅  最后修改于: 2021-05-30 04:11:35             🧑  作者: Mango

vector :: assign()是C++中的STL,它通过替换旧元素将新值分配给vector元素。如果需要,它也可以修改向量的大小。

分配常量值的语法:

vectorname.assign(int size, int value)

Parameters: 
size - number of values to be assigned
value - value to be assigned to the vectorname

程序1:下面的程序显示如何为向量分配常量值

CPP
// CPP program to demonstrate
// how to assign constant values to a vector
 
#include 
using namespace std;
 
int main()
{
    vector v;
    v.assign(7, 100);
 
    cout << "Size of first: "
      << int(v.size()) << '\n';
 
    cout << "Elements are\n";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    return 0;
}


CPP
// CPP program to demonstrate
// how to assign values to a vector
// from a list
 
#include 
using namespace std;
 
int main()
{
    vector v1;
    int a[] = { 1, 2, 3 };
 
    // assign first 2 values
    v1.assign(a, a + 2);
 
    cout << "Elements of vector1 are\n";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";
 
    vector v2;
    // assign first 3 values
    v2.assign(a, a + 3);
 
    cout << "\nElements of vector2 are\n";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";
 
    return 0;
}


CPP
// CPP program to demonstrate
// how to modify vector size
 
#include 
using namespace std;
 
int main()
{
    vector v;
    v.assign(7, 100);
 
    cout << "Size of first: " << int(v.size()) << '\n';
 
    cout << "Elements are\n";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
 
    // modify the elements
    v.assign(v.begin(), v.begin() + 3);
 
    cout << "\nModified VectorElements are\n";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    return 0;
}


C++
#include 
#include 
using namespace std;
int main()
{
    vector v;
     
    // Initialize v with an initialization list
    v.assign({ 1, 2, 3 });
    cout << "The list is:" << endl;
    for (auto i = v.begin(); i != v.end(); i++)
    {
        // Printing 1 2 3 as output
        cout << *i << " ";
    }
    return 0;
}


输出
Size of first: 7
Elements are
100
100
100
100
100
100
100

从数组或列表分配值的语法:

vectorname.assign(arr, arr + size)

Parameters: 
arr - the array which is to be assigned to a vector
size - number of elements from the beginning which has to be assigned.

程序2:下面的程序显示如何从数组或列表中分配值

CPP

// CPP program to demonstrate
// how to assign values to a vector
// from a list
 
#include 
using namespace std;
 
int main()
{
    vector v1;
    int a[] = { 1, 2, 3 };
 
    // assign first 2 values
    v1.assign(a, a + 2);
 
    cout << "Elements of vector1 are\n";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";
 
    vector v2;
    // assign first 3 values
    v2.assign(a, a + 3);
 
    cout << "\nElements of vector2 are\n";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";
 
    return 0;
}
输出
Elements of vector1 are
1 2 
Elements of vector2 are
1 2 3 

从向量修改值的语法

vectorname.assign(InputIterator first, InputIterator last) 

Parameters: 
first - Input iterator to the initial position range.
last - Input iterator to the final position range.

程序3:下面的程序显示了如何修改向量

CPP

// CPP program to demonstrate
// how to modify vector size
 
#include 
using namespace std;
 
int main()
{
    vector v;
    v.assign(7, 100);
 
    cout << "Size of first: " << int(v.size()) << '\n';
 
    cout << "Elements are\n";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
 
    // modify the elements
    v.assign(v.begin(), v.begin() + 3);
 
    cout << "\nModified VectorElements are\n";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    return 0;
}
输出
Size of first: 7
Elements are
100
100
100
100
100
100
100

Modified VectorElements are
100
100
100

用初始化器列表分配值的语法:

vectorname.assign((initializer_list)
Parameter: initializer_list

程序4:下面的程序显示如何分配带有初始化程序列表的向量。

C++

#include 
#include 
using namespace std;
int main()
{
    vector v;
     
    // Initialize v with an initialization list
    v.assign({ 1, 2, 3 });
    cout << "The list is:" << endl;
    for (auto i = v.begin(); i != v.end(); i++)
    {
        // Printing 1 2 3 as output
        cout << *i << " ";
    }
    return 0;
}
输出
The list is:
1 2 3 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”