本文介绍了声明&初始化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我可以单独声明并初始化字符串数组吗?如何以及在哪里 我会在下面的代码中执行此操作?它是使用Visual C ++ 2005创建的 Express Beta 2 ... 在C#中我会有 private string [] myArray; 及更高版本 myArray = new string [] {" element 1"," element 2"现在,我知道C ++比C#更难学,但....... 谢谢 Geoff #pragma一次 命名空间slider1 { using namespace System; using namespace System :: ComponentModel; using namespace System :: Collections; using namespace System :: Windows :: Forms; 使用命名空间System :: Data; 使用命名空间System :: Drawing; public ref class Form1:public System :: Windows :: Forms :: Form { public: Form1(无效) { InitializeComponent(); // // TODO:添加构造函数代码她e $> // } 受保护: ///< ;摘要> ///清理正在使用的所有资源。 ///< / summary> ///< param name =" disposing">" 参数的说明"< / param> virtual void Dispose(布尔处理)覆盖 { if(disposing&&组件) { 删除组件; } __super :: Dispose(disposing); } private:System :: Windows :: Forms :: PictureBox ^ pictureBox1; protected: private:System :: Windows :: Forms :: Button ^ button1; private:System :: Windows :: Forms :: Label ^ label1; private:System :: Windows: :Forms :: Label ^ label2; private:System :: Windows :: Forms :: TrackBar ^ trackBar1; private: / //< summary> ///所需的设计变量。 ///< / summary> System :: ComponentModel: :Container ^ components; #pragma region Windows窗体设计器生成的代码 ///< summary> // /支持Designer的必需方法 - 请不要使用代码编辑器修改 修改此方法的内容。 /// < / summary> void InitializeComponent(void) { Syste m :: ComponentModel :: ComponentResourceManager ^ resources =(gcnew System :: ComponentModel :: ComponentResourceManager(F orm1 :: typeid)); this-> pictureBox1 = (gcnew 系统:: Windows :: Forms :: PictureBox()); this-> button1 =(gcnew 系统: :Windows :: Forms :: Button()); this-> label1 =(gcnew System :: Windows :: Forms :: Label()); this-> label2 =(gcnew System :: Windows :: Forms :: Label()); this-> trackBar1 =(gcnew System :: Windows :: Forms :: TrackBar()); (cli :: safe_cast< System :: ComponentModel :: ISupportInialialize ^ this-> ResumeLayout(false); this-> PerformLayout(); } #pragma endregion private:System :: Void Form1_Load(System :: Object ^ sender, System :: EventArgs ^ e){ } }; } 解决方案 (cli :: safe_cast< System :: ComponentModel :: ISupportIn itialize ^ this-> SuspendLayout(); // // pictureBox1 // this-> pictureBox1-> Image = (cli :: safe_cast< System :: Drawing :: Image ^ this-> pictureBo x1-> Location = System :: Drawing :: Point(36,26); this-> pictureBox1-> Name = L" pictureBox1"; this-> pictureBox1-> Size = System :: Drawing :: Size(218,129); this-> pictureBox1-> TabIndex = 0; this-> pictureBox1-> ; TabStop = false; // // button1 // this-> button1-> Location = System :: Drawing :: Point( 105,231); this-> button1-> Name = L" button1"; this-> button1-> Size = System :: Drawing :: Size( 75,23); this-> button1-> TabIndex = 1; this-> button1-> Text = L" button1"; // // label1 // this-> label1-> AutoSize = true; this-> label1-> Location = System :: Drawing :: Point (23,190); this-> label1-> Name = L" label1"; this-> label1-> Size = System :: Drawing :: Size(31, 13); this-> label1-> TabIndex = 2; this-> label1-> Text = L" label1"; // // label2 // this-> label2-> AutoSize = true; this-> label2-> Location = System :: Drawing :: Point(222,190); this-> label2-> Name = L" label2"; this-> label2-> Size = System :: Drawing :: Size(31, 13); this-> label2-> TabIndex = 3; this-> label2-> Text = L" label2"; // // trackBar1 // this-> trackBar1-> Location = System :: Drawing :: Point(87,180); this-> trackBar1-> Name = L" trackBar1"; this-> trackBar1-> Size = System :: Drawing :: Size(104,45); this-> trackBar1-> TabIndex = 4; this-> trackBar1-> Scroll + = gcnew System :: EventHandler(this,& Form1 :: Form1_Load); // // Form1 // this-> AutoScaleDimensions = System :: Drawing :: SizeF(6,13); this-> AutoScaleMode = System :: Windows :: Forms :: AutoScaleMode :: Font; this-> ClientSize = System :: Drawing ::大小(292, 266); this-> Controls-> Add(this-> trackBar1); this-> Controls-> Add(this-> label2); this-> Controls-> Add(this-> label1); this-> Controls-> Add(this-> button1); this - > Controls-> Add(this-> pictureBox1); this-> Name = L" Form1"; this-> Text = L" Form1"; this->加载+ = gcnew System :: EventHandler(this,& Form1 :: Form1_Load); (cli :: safe_cast< System :: ComponentModel :: ISupportIn itialize ^ (cli :: safe_cast< System :: ComponentModel :: ISupportIn itialize ^ this-> ResumeLayout(false); this-> PerformLayout( ); } #pragma endregion private:System :: Void Form1_Load(System :: Object ^ sender, System :: EventArgs ^ e){} }; } 这是VC.Net 2003的等价物: String * myArray []; myArray = new String * [2]; myArray [0] = new String(" element 1"); myArray [1] = new String(" element 2"); 我应该告诉你我没有在这个盒子上安装2005(其中我有两个C ++编译器限制< g>)但我认为要从MC ++转换为 C ++ / CLI所有你需要做的就是替换*'用^'和新'用' gcnew'的 String ^ myArray []; myArray = new String ^ [2]; myArray [0] = gcnew字符串(元素1); myArray [1] = gcnew字符串("元素2"); 问候, 将 Hello,Can I separately declare and initialize a string array? How and wherewould I do it in the code below? It was created using Visual C++ 2005Express Beta 2 ...In C# I would haveprivate string[] myArray;and latermyArray = new string[] {"element 1","element 2"};Now, I know C++ is more difficult to learn than C# but .......ThanksGeoff#pragma oncenamespace slider1{using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;public ref class Form1 : public System::Windows::Forms::Form{public:Form1(void){InitializeComponent();////TODO: Add the constructor code here//}protected:/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">"description of theparameter"</param>virtual void Dispose(Boolean disposing) override{if (disposing && components){delete components;}__super::Dispose(disposing);}private: System::Windows::Forms::PictureBox^ pictureBox1;protected:private: System::Windows::Forms::Button^ button1;private: System::Windows::Forms::Label^ label1;private: System::Windows::Forms::Label^ label2;private: System::Windows::Forms::TrackBar^ trackBar1;private:/// <summary>/// Required designer variable./// </summary>System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do notmodify/// the contents of this method with the code editor./// </summary>void InitializeComponent(void){System::ComponentModel::ComponentResourceManager^ resources = (gcnewSystem::ComponentModel::ComponentResourceManager(F orm1::typeid));this->pictureBox1 = (gcnewSystem::Windows::Forms::PictureBox());this->button1 = (gcnewSystem::Windows::Forms::Button());this->label1 = (gcnewSystem::Windows::Forms::Label());this->label2 = (gcnewSystem::Windows::Forms::Label());this->trackBar1 = (gcnewSystem::Windows::Forms::TrackBar());(cli::safe_cast<System::ComponentModel::ISupportIn itialize^this->ResumeLayout(false);this->PerformLayout();}#pragma endregionprivate: System::Void Form1_Load(System::Object^ sender,System::EventArgs^ e) {}};} 解决方案 (cli::safe_cast<System::ComponentModel::ISupportIn itialize^ this->SuspendLayout(); // // pictureBox1 // this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^ this->pictureBox1->Location = System::Drawing::Point(36, 26); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(218, 129); this->pictureBox1->TabIndex = 0; this->pictureBox1->TabStop = false; // // button1 // this->button1->Location = System::Drawing::Point(105, 231); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 1; this->button1->Text = L"button1"; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(23, 190); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(31, 13); this->label1->TabIndex = 2; this->label1->Text = L"label1"; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(222, 190); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(31, 13); this->label2->TabIndex = 3; this->label2->Text = L"label2"; // // trackBar1 // this->trackBar1->Location = System::Drawing::Point(87, 180); this->trackBar1->Name = L"trackBar1"; this->trackBar1->Size = System::Drawing::Size(104, 45); this->trackBar1->TabIndex = 4; this->trackBar1->Scroll += gcnew System::EventHandler(this, &Form1::Form1_Load); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->trackBar1); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->button1); this->Controls->Add(this->pictureBox1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); (cli::safe_cast<System::ComponentModel::ISupportIn itialize^ (cli::safe_cast<System::ComponentModel::ISupportIn itialize^ this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; }This is the VC.Net 2003 equivalent:String *myArray[];myArray = new String*[2];myArray[0] = new String("element 1");myArray[1] = new String("element 2");I should tell you that I don''t have 2005 installed on this box (where I havea two C++ compiler limit <g>) but I think that to convert from MC++ toC++/CLI all you have to do is to replace the *''s with ^''s and new''s withgcnew''sString ^myArray[];myArray = new String^ [2];myArray[0] = gcnew String("element 1");myArray[1] = gcnew String("element 2");Regards,Will 这篇关于声明&初始化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!