1
class Code
{
public:
  Code();
  //string decode(vector< string> message);
private:
  //vector<string> codewords;
  //vector<char> alpha;
  char vector<char>  alphacode();
  string vector<string>  morsecode();
  //char decode(string c);

};

Code::Code()
{

}

string Code::vector<string> morsecode()
{
 vector<string> temp(28);
 temp[0] =".-";
 temp[1] ="-...";
 temp[2] ="-.-.";
 temp[3] ="-..";
 temp[4] =".";
 temp[5] ="..-.";
 temp[6] ="--.";
 temp[7] ="....";
 temp[8] ="..";
 temp[9] =".---";
 temp[10] ="-.-";
 temp[11] =".-..";
 temp[12] ="--";
 temp[13] ="-.";
 temp[14] ="---";
 temp[15] =".--.";
 temp[16] ="--.--";
 temp[17] =".-.";
 temp[18] ="...";
 temp[19] ="-";
 temp[20] ="..-";
 temp[21] ="...-";
 temp[22] =".--";
 temp[23] ="-..-";
 temp[24] ="-.--";
 temp[25] ="--..";
 temp[26] =".......";
 temp[27] ="x";
 return temp;
}

char Code::vector<char> alphacode()
{
 vector<char> temp;
 for (char c='A'; c<='Z'; c++)
      temp.push_back(c);
 temp.push_back(' ');
 temp.push_back('.');
 return temp;
}

int main()
{

return 0;
}

这是我到目前为止的代码,但我不知道我的向量类的语法是否正确。另外,我将如何访问代码的 int main() 部分中的向量。抱歉,C++ 的第一季度对这一切还是很陌生。

4

2 回答 2

0

首先,您需要返回一个向量类型

vector<char>  Code::alphacode()
{
    //
}

vector<string>  Code::morsecode()
{
    //
}

其次,访问向量,迭代器是一个不错的选择:

int main()
{
    Code c;
    vector<string> strVector = c.morsecode();
    vector<string>::iterator it = strVector.begin();
    for (; it != strVector.end(); ++it) {
        // accessing every single string inside this vector by dereference *it eg.
        cout << *it << endl;
    }
}

运行代码,检查结果,你会得到它:P

于 2012-10-23T03:15:27.880 回答
0
char vector<char>  alphacode();
string vector<string>  morsecode();

成员函数的返回类型错误。char,vector<char>是两种不同的类型,不能同时使用。从代码来看,您似乎需要vector<char>, vector<string>

vector<string> Code::morsecode()
{
    // ...
}


vector<char> Code::alphacode()
{
    // ...
}
于 2012-10-23T01:21:07.673 回答