首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量Initializer_list

向量Initializer_list
EN

Stack Overflow用户
提问于 2019-01-05 04:10:52
回答 1查看 80关注 0票数 0

我正在尝试使用我自己的类实现一个向量,所以我不能完成初始化器列表部分

代码语言:javascript
复制
# include <iostream>
# include <exception>
# include<initializer_list>
template <class T>
 class vector {
  public:
  T* a;
T n;
int pos, c;
vector() { a = 0; n = 0; }
vector(T n) : n(n) { a = new T[n]; }
vector(std::initializer_list <vector> l) {
    a = new T[int(sizeof(l))];
        for (int f : l)
            *(a + f) = l.begin() + f;
     }
 void push_back(T k) {
    int i = k;
    *(a + n) = k;
}
 vector& operator= (vector&& th) {
     this->a = th.a;
    th.a = nullptr; return (*this);
}
vector& operator=(vector& k)
{
    this->a = k.a;
    return(*this);
}
int  size() { return n; }
void pop_back() { *(a + n) = nullptr;
n--;
}
void resize(int c) {  
    delete a;
    n = c;
 a = new T[c];
 }
 T operator[](int pos) {
    if (pos > sizeof(a))
        std::cout << "out of range";

    else return *(a + pos);
 }
 };
 int main() {
vector<int> a(10);
vector<char>b{ 'w','b','f','g' };
getchar();
return 0;

}

我只是尝试使用指针偏移量表示法将初始化器列表项放入动态数组中,但我收到错误VS 17 IDE

代码语言:javascript
复制
Severity    Code    Description Project File    Line    Suppression   
Error   C2440   '=': cannot convert from 'const _Elem *' to 'T' 
Error   C2440   'initializing': cannot convert from 'const _Elem' to 'int'
EN

回答 1

Stack Overflow用户

发布于 2019-01-05 23:55:32

你好,尼姆罗德!

代码语言:javascript
复制
#include <iostream>
#include <exception>
#include <initializer_list>
// normally people don't place a space between '#' and 'include'

template <class T>
class vector {
public:
    T* a;
    int n;
    // probably it is length the vector
    // change T to int
    int pos, c;
    vector() { 
        a = nullptr;
        // do not use '0' to instruct null pointer 
        n = 0; 
    }

    vector(int n): n(n) { a = new T[n]; }

    vector(std::initializer_list<T> l) {
        a = new T[l.size()];
        // redundant force cast from size_t to int

        for (int i = 0; i < l.size(); i++) {
            a[i] = l.begin()[i];
        }
        // for (int f : l) # it seems that you wrote JavaScript before?
        //     *(a + f) = l.begin() + f;
    }

    void push_back(T k) {
        // assigns "T k" to "int i"? it's confusing

        // int i = k;
        // *(a + n) = k;
    }

    // probably still many problems
    vector& operator=(vector&& th) {
        this->a = th.a;
        th.a = nullptr;
        return (*this);
    }

    vector& operator=(vector& k) {
        this->a = k.a;
        return(*this);
    }

    int size() { return n; }

    void pop_back() { *(a + n) = nullptr;
        n--;
    }

    void resize(int c) {  
        delete a;
        n = c;
        a = new T[c];
    }

    T operator[](int pos) {
        if (pos > sizeof(a))
            std::cout << "out of range";
        else return *(a + pos);
    }
 };

int main() {
    vector<int> a(10);
    vector<char>b{ 'w','b','f','g' };
    getchar();
    return 0;
}

你仍然需要更多的练习。XP

在你的代码上下文中,int.

  • Range for loop的模板变量应该是T而不是initializer_list<T>initializer_list<T>会在
  1. 中取值。因此,它应该属于T.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54045403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档