0

関数を として宣言しようとするとvoid some_function(vector<pair<int, int> > theVector)、エラーが発生します (おそらく " ." の後のコンマから)pair<intこのベクトルをペアで関数に渡す方法についてのアイデアはありますか?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

void someFunc(int x, int y, vector<pair<int, int> > hello);

int main()
{
    int x = 0;
    int y = 5;

    vector<pair<int, int> > helloWorld;
    helloWorld.push_back(make_pair(1,2));

    someFunc(x,y,helloWorld);
}

void someFunc(int x, int y, vector<pair<int, int> > hello)
{
    cout << "I made it." << endl;
}

エラー: 'vector' が宣言されていません

4

4 に答える 4

4

<utility>を定義する のインクルードに失敗しstd::pairandの代わりにvectorandを使用しています。pairstd::vectorstd::pair

すべての標準テンプレート ライブラリは名前空間内にあるため、STL の型の前に などstdを付ける必要があります。別の方法は、を含めた後に追加することです。stdstd::vectorusing std::vector;<vector>

于 2013-01-31T03:45:57.963 に答える
4

vector、pair、make_par の完全な名前空間を提供する必要があります。これらは std 名前空間からのものです。

void someFunc(int x, int y, std::vector<std::pair<int, int> > hello);

int main()
{
    int x = 0;
    int y = 5;

    std::vector<std::pair<int, int> > helloWorld;
    helloWorld.push_back(std::make_pair(1,2));

    someFunc(x,y,helloWorld);
    return 0;
}

void someFunc(int x, int y, std::vector<std::pair<int, int> > hello)
{
    std::cout << "I made it." << std::endl;
}

補足: vector を参照によって someFunc に渡すことができます。これにより、不要なコピーが省略されます。

 void someFunc(int x, int y, const std::vector<std::pair<int, int> >& hello);
                              ^^^                                   ^^
于 2013-01-31T03:58:32.170 に答える
1

<vector>とを含めました<utility>か? と の両方でstd::名前空間を使用する必要があります。vectorpair

例えば。 void some_function(std::vector< std::pair<int, int> > theVector)

編集:もちろん、通常はベクトルを値で渡すのではなく、参照で渡す必要があります。

例えば。 void some_function(std::vector< std::pair<int, int> >& theVector)

于 2013-01-31T03:46:05.727 に答える
0

あなたのコードをチェックしました。std名前空間をあなたのすぐ下に追加するだけ#includeです。また、追加する必要はありません#include <utility>。それがなくても機能します。
using namespace std

于 2015-04-27T11:22:00.393 に答える