C++のinsert()関数の挙動を理解する

はじめに

このチュートリアルでは、C++のvectorのinsert()について学んでいきます。また、それがどのように機能し、例を用いてさまざまな方法で挿入操作を実行するために使用することができるかも見ていきます。

C++におけるvector::insert()関数

基本的に、C++のSTLであるvector::insert()関数は、要素または値をvectorコンテナに挿入するために使用されます。一般的に、この関数は挿入された要素の最初を指すイテレータを返します。

ベクトルでinsert()関数を使用する方法

insert()メソッドは、さまざまな方法で単一または複数の要素を与えられたベクトルに挿入するために使用することができます。任意の位置に単一の値を挿入することができ、さらに複数の値を一度にベクトルに挿入することもできます。また、別のベクトルから複数の値をまとめて挿入することも可能です。

では、それを簡単に行う方法を見てみましょう。

ベクトルに単一の値を挿入してください。

私たちは、ベクトルを変更するために、挿入したい位置を指すイテレータとそこに挿入する値を直接insert()関数に渡すことができます。

以下の例を注意深く見てください。ここでは、ベクトルの始めに値10を挿入しようとしています。

#include<iostream>    
#include<vector> 
using namespace std;
int main()
{  
	vector<int> vec {1,2,3,4,5};
	
	cout<<"Intially vector: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	
	vec.insert(vec.begin(),10);//Inserting 10 to the vector
	
	cout<<"\n\nThe modified vector is: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	return 0;
}

出力:

Intially vector:  1 2 3 4 5

The modified vector is:  10 1 2 3 4 5

ここでは、

  • Firstly we initialize a vector, vec. And print the same,
  • Then we call the insert() function on the vector vec with parameters vec.begin() and 10(new value). Note, here vec.begin() returns an iterator pointing to the start of the vector,
  • After the insertion has been done we print the new vector using a simple for loop to see the resultant vector.

同じ値を複数回挿入する

私たちは、insert()関数を使用して一度に複数の値をベクターに挿入することもできます。これは、挿入したい開始位置を示すイテレーター、値が繰り返される回数、そして最後に値を渡すことで実行できます。

以下の例は、適切な使用方法を説明しています。

#include<iostream>    
#include<vector> 
using namespace std;
int main()
{  
	vector<int> vec {10,20,30,40};
	
	cout<<"Intially vector: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	
	vec.insert(vec.end(),3,100);//Inserting 100, 3 times to the vector
	
	cout<<"\n\nThe modified vector is: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	return 0;
}

出力;

Intially vector:  10 20 30 40

The modified vector is:  10 20 30 40 100 100 100

ここでは

  • We initialize our vector vec and print the same,
  • Then we pass an iterator pointing to the end of the vector, as returned by vec.end(), 3(the number of times we want the value to repeat), and the value 100 to the insert() function.
  • In this way, as we can observe from the output, 100 is inserted thrice at the end of the vector, vec.

3. 別のベクトルを挿入してください。

さらに、古いベクトルに別のベクトルの要素を挿入することもできます。ただし、別のベクトルを挿入する位置を指すイテレーターを、古いベクトルに渡す必要があります。それに加えて、2番目のベクトルの始点と終点を指すイテレーターも必要です。

僅かな例を取り上げることで、仕組みを理解しましょう。

#include<iostream>    
#include<vector> 
using namespace std;
int main()
{  
	vector<int> vec {2,4,6,8};
	vector<int> vec2 {1,3,5,7};
	cout<<"Intially first vector: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	cout<<"\nIntially second vector: ";
	for(auto i=vec2.begin(); i<vec2.end(); i++)
	{
		cout<<" "<<*i;
	}
	
	//Inserting vec2 at the beginning of the vec vector
	vec.insert(vec.begin(),vec2.begin(),vec2.end());
	
	cout<<"\n\nThe modified vector is: ";
	for(auto i=vec.begin(); i<vec.end(); i++)
	{
		cout<<" "<<*i;
	}
	return 0;
}

結果;

Intially first vector:  2 4 6 8
Intially second vector:  1 3 5 7

The modified vector is:  1 3 5 7 2 4 6 8

ここでは、vecとvec2という2つのベクトルがあります。その中で、vec2はベクトルvecに挿入する必要がある要素です。前述した適切なパラメータでinsert()関数を呼び出します。これにより、ベクトルvecが変更され、2番目のベクトルの要素が先頭に挿入されます。

結論

では、このチュートリアルでは、C++のSTLのベクトルinsert()関数の動作と使用方法を説明しました。より理解を深めるために、上記のコードスニペットを自分で試してみることをお勧めします。何か質問があれば、お気軽に以下にコメントしてください。

参考文献

  • Replace an element into a specific position of a vector – Stack Overflow Question,
  • How to insert an element into the beginning of a vector? – Stack Overflow Question,
  • Vectors in C++ – JournalDev Tutorial.
コメントを残す 0

Your email address will not be published. Required fields are marked *