AI 驱动的代码生成已成为软件开发中日益重要的一环,它利用先进的模型来编写、优化和理解代码。然而,AI 并非要取代开发者,而是协助他们工作。最终,如何使用 AI 的建议取决于人类自己。让我们看看我们向 GPT-4o 提出的这个查询:
给我一个将两个大整数相加的 C++ 函数
以下是回答:
要在 C++ 中将两个大整数相加,您可以使用string数据类型来处理任意大的数。下面是一个将以字符串表示的两个大整数相加的函数:
#include <iostream>
#include <string>
#include <algorithm>
// Function to add two big integers
std::string addBigIntegers(const std::string& num1, const std::string& num2) {
std::string result = "";
int carry = 0;
// Make sure num1 is the longer string
std::string n1 = num1.size() >= num2.size() ? num1 : num2;
std::string n2 = num1.size() < num2.size() ? num1 : num2;
// Reverse the strings to make addition easier
std::reverse(n1.begin(), n1.end());
std::reverse(n2.begin(), n2.end());
// Add digit by digit
for (size_t i = 0; i < n1.size(); ++i) {
int digit1 = n1[i] - '0';
int digit2 = i < n2.size() ? n2[i] - '0' : 0;
int sum = digit1 + digit2 + carry;
carry = sum / 10;
result.push_back((sum % 10) + '0');
}
// If there is a carry left, add it
if (carry) {
result.push_back(carry + '0');
}
// Reverse the result to get the final sum
std::reverse(result.begin(), result.end());
return result;
}
int main() {
std::string num1, num2;
// Prompt the user for the first big integer
std::cout << "Enter the first big integer: ";
std::cin >> num1;
// Prompt the user for the second big integer
std::cout << "Enter the second big integer: ";
std::cin >> num2;
// Call the addBigIntegers function and store the result
std::string sum = addBigIntegers(num1, num2);
// Output the result
std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << "." << std::endl;
return 0;
}
这个回答的问题在于它对查询的理解方式。在这个例子中,GPT 给出了一个适合处理极大数值的解决方案,类似于网上针对此类情况的许多方案。然而,如果开发者对"大数"的定义只包括不超过十亿的数值呢?
在这个场景中,由于缺乏明确定义的上下文,我们得到了一个过度设计的方案,它可能会不必要地使代码复杂化。在使用 AI 生成代码时,上下文起着至关重要的作用,这凸显了精确说明需求而非给出笼统指令的重要性。
让我们再看一个例子,以凸显上下文的重要性。
给我一段从文件存取对象的 C++ 代码
回答是:
#include <iostream>
#include <fstream>
#include <string>
class Person {
public:
std::string name;
int age;
// Constructor
Person() : name(""), age(0) {}
Person(const std::string& n, int a) : name(n), age(a) {}
// Method to display the person's information
void display() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
// Friend functions for serialization and deserialization
friend std::ofstream& operator<<(std::ofstream& ofs, const Person& p);
friend std::ifstream& operator>>(std::ifstream& ifs, Person& p);
};
// Overload the << operator for writing a Person object to a file
std::ofstream& operator<<(std::ofstream& ofs, const Person& p) {
ofs << p.name << std::endl;
ofs << p.age << std::endl;
return ofs;
}
// Overload the >> operator for reading a Person object from a file
std::ifstream& operator>>(std::ifstream& ifs, Person& p) {
std::getline(ifs, p.name);
ifs >> p.age;
ifs.ignore(); // Ignore the newline character after reading age
return ifs;
}
以及 main 函数:
int main() {
// Create a Person object
Person p1("John Doe", 30);
// Save the object to a file
std::ofstream ofs("person.txt");
if (ofs.is_open()) {
ofs << p1;
ofs.close();
std::cout << "Person saved to file." << std::endl;
} else {
std::cerr << "Error opening file for writing." << std::endl;
}
// Retrieve the object from the file
Person p2;
std::ifstream ifs("person.txt");
if (ifs.is_open()) {
ifs >> p2;
ifs.close();
std::cout << "Person read from file:" << std::endl;
p2.display();
} else {
std::cerr << "Error opening file for reading." << std::endl;
}
return 0;
}
在这个例子中,我们遇到了另一个问题:代码为完成任务重新造了轮子,导致代码行数更多,复杂性也随之增加。
然而,如果我们提出同样的查询并附上更多说明:
给我一段使用最著名的可用库从文件存取对象的 C++ 代码
在这种情况下,回答使用了 Boost 库:
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
总而言之,在使用 AI 生成 C++ 代码时,永远不要低估上下文的重要性。它是获得贴切回答的关键。因此,请密切关注您的查询——这才是秘诀所在!
