簡體   English   中英

C ++ 11 for Template函數中的循環

[英]C++11 for loop in a Template Function

我正在嘗試編寫一個在控制台上打印數據的功能。 該函數是模板化的,因為它應該接受不同類型的數據。

代碼如下所示:

template<typename DataType>
void PrintData(DataType *X)
{
    for (DataType Data : X)
    {
        cout << Data << "\t";
    }
    cout << endl;
}

int main()
{

    int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
    double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };

    PrintData(nArray);
    PrintData(dArray);

    system("pause");
    return EXIT_SUCCESS;
}

我得到一個錯誤,即模板化函數PrintData中的變量Data未聲明

error C2065: 'Data' : undeclared identifier 
error C3312: no callable 'begin' function found for type 'double *'    
error C3312: no callable 'begin' function found for type 'int *'    
error C3312: no callable 'end' function found for type 'double *'    
error C3312: no callable 'end' function found for type 'int *'  

任何幫助,將不勝感激。 謝謝

假設您已經包含了iostream頭文件和using namespace std; 指示。 那么你的問題是:

  1. 您不應該使用DataType * 你的代碼使X成為一個指針,它與數組不同。 請改用DataType const&DataType&&
  2. 您必須包含iterator頭文件,該文件為C樣式數組提供beginend函數。

以下代碼適用於我。

#include <iostream>
#include <iterator>
using namespace std;

template<typename DataType>
void PrintData(DataType const& X)
{
    for (auto Data : X)
    {
        cout << Data << "\t";
    }
    cout << endl;
}

int main()
{

    int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
    double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };

    PrintData(nArray);
    PrintData(dArray);

    return EXIT_SUCCESS;
}

正如Igor Tandetnik評論的那樣,如果要引用數組的大小,可以使用template<struct DataType, size_t N>

更新:

  1. 使用template<struct DataType>DataType *XDataType被推導為intdouble ,而X是不是容器的指針。
  2. 使用template<struct DataType, size_t N>DataType (&X)[N]DataType被推導為intdouble ,而X是一個可以與基於范圍的for循環一起使用的數組。
  3. 使用template<struct DataType>DataType&& XDataType const& XDataType被推導為int[7]double[5]X也是一個數組。

問題是你傳遞的是數組的名稱(例如, nArray ),它只是一個指向第一個元素的指針。 但是,新式for循環期望您可以調用beginend - 一個范圍。

以下更改使其可以使用vector而不是數組。 請注意,除此之外幾乎沒有什么區別,並且通常很少有理由在當代C ++中使用C風格的數組。

#include <iostream>                                                                                                                                                                                          
#include <vector>

using namespace std;

template<typename DataType>
void PrintData(const DataType &X)
{
    for (const auto &Data : X)
    {
        cout << Data << "\t";
    }
    cout << endl;
}

int main()
{

    vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
    vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };

    PrintData(nArray);
    PrintData(dArray);

    system("pause");
    return EXIT_SUCCESS;

你有一些問題:

1)DataType必須是容器。 所以,嘗試使用:

std::vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
std::vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };

2)你不會改變容器。 最好通過const引用傳遞容器:

template<typename DataType>
void PrintData(const DataType & X);

3)像這樣改變循環:

for (const auto & value : X) {
    std::cout << value << "\t";
}

代碼示例:

#include <iostream>
#include <vector>

template<typename DataType>
void PrintData(const DataType & X) {
    for (const auto & value : X) {
        std::cout << value << "\t";
    }
    std::cout << std::endl;
}

int main() {
    std::vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
    std::vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };
    PrintData(nArray);
    PrintData(dArray);
    return 0;
}

正如Igor建議的那樣,如果你想使用DataType *那么你需要做這樣的事情

#include <iostream>
#include <iterator>
using namespace std;

template <typename DataType, size_t N> 
void PrintData(DataType (&X)[N])
{
    for (auto i : X)
        cout << i << "\t";

    cout << endl;
}

int main()
{    
    int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
    double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };

    PrintData(nArray);
    PrintData(dArray);

    return EXIT_SUCCESS;
}

產量

7   5   4   3   9   8   6   
4.3 2.5 -0.9    100.2   3   

說明:

如果你看到void PrintData(int* nArray); void PrintData(int (&nArray)[7] ); 是類似的聲明,除了秒一告訴數組結束的位置。

模板功能

template <typename DataType, size_t N> 
void PrintData(DataType (&X)[N])

被推斷為'

void PrintData(int (&nArray)[7] )

你也可以寫

void PrintData(int (&nArray)[7] )
{
    for (auto i : nArray)
        cout << i << "\t";

    cout << endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM