简体   繁体   English

将std :: vector转换为char *

[英]Converting std::vector to char*

I am a c++ student working on a project to receive and encode a message with a simple cipher. 我是一名从事项目研究的c ++学生,它使用简单的密码来接收和编码消息。 To do so, the program accepts each word as a string, converts the string into a vector of characters, and then modifies each character before storing it in a file. 为此,该程序接受每个单词作为字符串,将字符串转换为字符向量,然后修改每个字符,然后再将其存储在文件中。 In the line that returns the encoded message to the main function, I get this error message: < cannot convert std::vector<char, std::allocator<char> >' to char*' for argument 1' to char* cipher(char*, int)' . 在将编码后的消息返回到主函数的行中,我收到以下错误消息:<无法将std::vector<char, std::allocator<char> >' to char *',参数1' to char *密码(char *,int)'。 Despite this error, the program will run, however, it will simply stop after a word has been entered and end the program. 尽管出现此错误,程序仍将运行,但是,它会在输入一个单词后立即停止并结束程序。 This is the code I am using: 这是我正在使用的代码:

// Cipher.cpp
// This program accepts a message and encodes it 
// Written by Will Grindle

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<math.h>
#include<fstream.h>
#include<vector.h>

using namespace std;

string get_message(); // function prototypes
char* cipher(char broken[], int length);

int main()
{
   int index, length; // declare index for vectors

   string message; // declare string

   ofstream outfile; // declare filestream

   outfile.open("MESSAGE.DAT", ios::in); // create file and open it

   message = get_message(); // use function to retrieve data

   length = message.length(); // find length of message

   // declare vector for breaking the strings down
   vector <char> broken(message.begin(), message.end()); 

   vector <char> encoded(50); // declare vector for encoded message

   encoded[index] = cipher(broken, length); // initialize encoded to new         message

   for(index = 0; index <= length - 1; index++)// loop for displaying values
   {
      cout << encoded[index] << endl;
   }

   if(outfile) // if there is no error with the file
   {
      for(index = 0; index <= length - 1; index++) // loop for writing encoded
      {                                            // message to file
         outfile << encoded[index] << endl;
         cout << "Data written to file." << endl;
      }
   }
   else
   {
      cout << "Error opening file." << endl; 
   }         

   outfile.close(); // close file

   system("pause");
   return 0;
}

string get_message()
{
   string entry; // declare string

   cout << "Please enter the word to be entered." << endl; // request entry
   cin >> entry; // user enters word

   system ("pause");

   return entry;
}

char* cipher(char broken[], int length)
{
   char index; // declare index

   if( broken[index] < 123 && broken[index] > 96 ) // if characters are lowercase, 
   {                                               // make them uppercase
      broken = broken - 32;
   }

   for(index = 0; index <= length - 1; index ++)
   {
      broken[index] = broken[index] * (2/3); 
      broken[index] = broken[index] - 12;
      broken[index] = broken[index] ^ 2;
   }

   cout << "Message encoded." << endl;

   system ("pause");

   return(broken);
} 

I am open to any suggestions on how to make this work. 我愿意就如何进行这项工作提出任何建议。 Thank you! 谢谢!

In C++, a vector isn't actually an array of chars, so you can't pass it into a function expecting a char array. 在C ++中,向量实际上不是char数组,因此您不能将其传递给需要char数组的函数。 You have a few options here. 您在这里有一些选择。

First, I'd recommend updating your code so that the cipher function takes in as a parameter either a vector or a std::string. 首先,我建议您更新代码,以使cipher函数将向量或std :: string作为参数。 These objects are safer than raw arrays because they know their sizes and can do reallocations if you need them to grow. 这些对象比原始数组更安全,因为它们知道它们的大小,并且可以根据需要进行重新分配。

Second, you could change the call to cipher to pass in a pointer to the vector's underlying array, like this: 其次,您可以更改对cipher的调用,以传递指向向量基础数组的指针,如下所示:

cipher(broken.data(), broken.size());

I think this is a less elegant and more error-prone solution, but you're welcome to use it if you'd like. 我认为这是一个不太优雅且更容易出错的解决方案,但是如果您愿意,欢迎使用它。

Your code looks more like C... 您的代码看起来更像C ...

Maybe start learning the basic data types and data structures first. 也许首先要开始学习基本数据类型和数据结构。

Then make sure you understand the usage of the vector template 然后确保您了解矢量模板的用法

This seems good as well: Differences between C and C++ It also points out some things about style and what you could do, but shouldn't do. 这似乎也很不错: C和C ++之间的差异它还指出了有关样式的一些内容以及您可以做但不应该做的事情。

And of course this Programmers.StackExChange Post: What are the fundamental differences between C and C++? 当然还有这个Programmers.StackExChange帖子: C和C ++之间的根本区别是什么?

Try to use a modern C++ IDE such as CodeBlocks, Eclipse, Netbeans, ...; 尝试使用现代的C ++ IDE,例如CodeBlocks,Eclipse,Netbeans等。 This will help you to prevent some faults at the beginning. 这将帮助您从一开始就预防某些故障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM