【问题标题】:vector assign issue in template of c++c++模板中的向量分配问题
【发布时间】:2022-01-05 23:27:52
【问题描述】:

我正在编写一个程序来使用向量实现队列。我正在使用该类作为模板。在主函数中,我试图根据模板数据类型创建字符串向量和 int 向量。但是我从向量分配方法中得到编译错误。

template <class T>
class queueWithArray {
private:
    vector<T> queueArray;
    int numberOfElements;
    int size;
    int head;
    int tail;

public:

    queueWithArray(int n) {
        numberOfElements = 0;
        head = -1;
        tail = -1;
        size = n;
        if(is_same<T,string>::value) {
            cout << "data type is string" << endl;
            queueArray.assign(n,"");
        } else {
            queueArray.assign(n,0);
        }
    }

...

int main() {
    string InputArray[] = {"to", "be", "or", "not", "to", "-", "be", "-", "-", "that", "-", "-", "-", "is"};
    queueWithArray<string> obj(2);
    for(auto i=0; i < (signed int)(sizeof(InputArray)/sizeof(InputArray[0])); ++i) {
        if(InputArray[i] == "-") {
            string item = obj.dequeue();
            cout << "dequeue->" << item << endl;
        } else {
            obj.enqueue(InputArray[i]);
            cout << "enqueue->" << InputArray[i] << endl;
        }

        obj.printQueue();

    }

    int InputArray_int[] = {10,20,30,40,50,-1,20,-1,-1,60,-1,-1,-1,70};
    queueWithArray<int> obj_int(2);
    for(auto i=0; i < (signed int)(sizeof(InputArray_int)/sizeof(InputArray_int[0])); ++i) {
        if(InputArray_int[i] == -1) {
            int item = obj_int.dequeue();
            cout << "dequeue->" << item << endl;
        } else {
            obj_int.enqueue(InputArray_int[i]);
            cout << "enquque->" << InputArray_int[i] << endl;
        }

        obj.printQueue();
    }
    return 0;
}

..\QueueUsingTemplate.cpp:135:31:从这里需要 ..\QueueUsingTemplate.cpp:45:4: 错误:没有匹配函数调用 >'std::vector::assign(int&, const char [1])' queueArray.assign(n,""); ^~~~~~~~~~ 在 c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\vector:64:0 包含的文件中, 来自 c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\queue:61, 来自 c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits\stdc++.h:86, 来自 ..\QueueUsingTemplate.cpp:18: c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:489:7: 注意:候选:void >std::vector<_tp _alloc>::assign(std: :vector<_tp _alloc>::size_type, const value_type&) [with >_Tp = int; _Alloc = std::allocator; std::vector<_tp _alloc>::size_type = unsigned int; >std::vector<_tp _alloc>::value_type = int] 分配(size_type __n,常量 value_type& __val) ^~~~~~

【问题讨论】:

标签: c++ gcc vector


【解决方案1】:

非常感谢 VainMan。 queryArray.assign(n, T{});解决了这个问题。嗨 Louis,请在此处找到包含语句的完整程序。

/*
 * queueUsingArray.cpp
 *
 *  Created on: 02-Nov-2021
 *      Author: Admin
 */

#include <iostream>
#include <ctype.h>
#include <bits/stdc++.h>
#include <vector>
#include <typeinfo>
#include <type_traits>

using namespace std;

template <class T>
class queueWithArray {
private:
    vector<T> queueArray;
    int numberOfElements;
    int size;
    int head;
    int tail;

public:

    queueWithArray(int n) {
        numberOfElements = 0;
        head = -1;
        tail = -1;
        size = n;
        queueArray.assign(n,T{});
    }

    void enqueue(T item) {
        if(numberOfElements == size) {
            resize(size * 2);
        }
        if((head == -1) && (tail == -1)) {
            head = tail = 0;
        } else {
            tail = (tail + 1) % size;
        }
        queueArray[tail] = item;
        numberOfElements++;
    }

    T dequeue() {
        T item;
        if(numberOfElements == 0) {
            cout << "No elements to dequeue" << endl;
        } else {
            if(numberOfElements == size/4) {
                resize(size/2);
            }
            item = queueArray[head];
            if(head == tail) {
                head = tail = -1;
            } else {
                head = (head + 1) % size;
            }
            numberOfElements--;
        }
        return item;
    }

    bool isEmpty() {
        return ((head == -1) && (tail == -1));
    }

    void resize(int newSize) {
        if(newSize > 0) {
            size = newSize;
            int newIndex = 0;
            for(int i=head; i<=tail; ++i){
                queueArray[newIndex] = queueArray[i];
                newIndex++;
            }
            queueArray.resize(newSize);
            head=0;
            tail=newIndex-1;
        } else {
            return;
        }
    }

    void printQueue() {
        if(!isEmpty()) {
            for(auto i=head; i<tail; i++) {
                cout << queueArray[i] << "->";
            }
            cout << queueArray[tail] << endl;
        } else {
            cout << "Queue is Empty" << endl;
        }
    }
};

int main() {
    string InputArray[] = {"to", "be", "or", "not", "to", "-", "be", "-", "-", "that", "-", "-", "-", "is"};
    queueWithArray<string> obj(2);
    for(auto i=0; i < (signed int)(sizeof(InputArray)/sizeof(InputArray[0])); ++i) {
        if(InputArray[i] == "-") {
            string item = obj.dequeue();
            cout << "dequeue->" << item << endl;
        } else {
            obj.enqueue(InputArray[i]);
            cout << "enqueue->" << InputArray[i] << endl;
        }

        obj.printQueue();

    }

    int InputArray_int[] = {10,20,30,40,50,-1,20,-1,-1,60,-1,-1,-1,70};
    queueWithArray<int> obj_int(2);
    for(auto i=0; i < (signed int)(sizeof(InputArray_int)/sizeof(InputArray_int[0])); ++i) {
        if(InputArray_int[i] == -1) {
            int item = obj_int.dequeue();
            cout << "dequeue->" << item << endl;
        } else {
            obj_int.enqueue(InputArray_int[i]);
            cout << "enquque->" << InputArray_int[i] << endl;
        }

        obj_int.printQueue();
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多