如何初始化数组向量

时间:2015-10-20 03:21:45

标签: c++ arrays vector mfc

我想创建一个数组向量

vector<BYTE[6]> cKey;
BYTE keyTemp[6];
memset(keyTemp, 0xFF, sizeof(keyTemp)); // Key is FFF...FF
cKey.push_back(keyTemp);

但是在push_back函数中,显示错误

Error C3074: an array can only be initialized with an initializer-list

如何初始化数组向量?

2 个答案:

答案 0 :(得分:2)

你做不到。 package com.example.mypc.thisissample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import com.firebase.client.Firebase; public class MainActivity extends AppCompatActivity { private EditText editText; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Firebase.setAndroidContext(this); final Firebase firebase = new Firebase("https://example.firebaseio.com/"); editText = (EditText) findViewById(R.id.editText); listView = (ListView) findViewById(R.id.listView); listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat"))); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message message = new Message(); message.setMessage(editText.getText().toString()); message.setAuthor("Name"); firebase.child("chat").push().setValue(message); editText.setText(""); } }); } } 使用的基础类型必须是 CopyAssignable ,而数组则不是。参考:http://en.cppreference.com/w/cpp/container/vector

解决方案是使用std::array作为基础类型,而不是原始数组:

std::vector

答案 1 :(得分:-1)

我不知道您使用BYTE的数据类型。这里是如何使用int数组。

#include <iostream>
#include <vector>

    int main()
    {
        std::vector<int*> cKey;
        int keyTemp[8] = { 1,2,3,4,5,6,7,8 };
        cKey.push_back(keyTemp);
        std::cout << cKey[0][6] << std::endl;
        system("pause");
        return 0;
    }