将两个矢量相加
Adding two vectors together
本文关键字:两个 更新时间:2023-10-16
所以我在构建代码时遇到了这个问题。这是的问题
这项工作是基于运算符重载的,您需要构建一个字符串计算器,计算器可以为字符串变量执行加减函数(只有
字符和字符串的空格)。
我遇到的问题是,当我试图将我创建的两个向量相加时。例如,向量A=<1,2,3>和向量B=<1,2>。我希望A+B等于<2,4,3>。但当我这样做的时候,我得到的输出是2。这是我的密码。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string a;
string b;
int k, j, ab, x;
vector <int> scab;
int main() {
cout << "Input A: ";
getline(cin, a);
cout << "Input B: ";
getline(cin, b);
vector<int> sca;
vector<int> scb;
// For A
for (int i = 0; i < a.size(); i++) {
sca.push_back(static_cast <int> (a[i]));
}
cout << "Input A: ";
for (int j = 0; j < sca.size(); ++j)
{
cout << sca[j] << "t";
}
cout << endl;
cout << endl;
// For B
for (int p = 0; p < b.size(); p++) {
scb.push_back(static_cast <int> (b[p]));
}
cout << "Input B: ";
for (int j = 0; j < scb.size(); ++j)
{
cout << scb[j] << "t";
}
scab.push_back(sca[j] + scb[j]);
cout << endl;
cout << endl;
cout << "A+B: " << scab[j] << "t";
system("pause");
}
提前感谢。
尝试使用标准库中的更多内容以使其更容易:
auto size = std::max(sca.size(), scb.size());
sca.resize(size);
scb.resize(size);
auto scab = std::vector<int>(size);
std::transform(sca.begin(), sca.end(), scb.begin(), scab.begin(), std::plus<int>());
从代码中可以看出,矢量包含表示数字的字符。此外,您还应该考虑可能发生的溢出。
这里有一个演示程序,展示了如何为这样的向量重载这样的运算符cna。
#include <iostream>
#include <algorithm>
#include <vector>
std::vector<int> operator +( const std::vector<int> &a, const std::vector<int> &b )
{
auto less = []( const std::vector<int> &a, const std::vector<int> &b )
{
return a.size() < b.size();
};
std::vector<int> c( std::min( a, b, less ) );
c.resize( std::max( a.size(), b.size() ), '0' );
int overflow = 0;
auto plus = [&overflow]( int x, int y )
{
int sum = x - '0' + y - '0' + overflow;
overflow = sum / 10;
return sum % 10 + '0';
};
std::transform( c.begin(), c.end(), std::max( a, b, less ).begin(), c.begin(), plus );
if ( overflow ) c.push_back( overflow + '0' );
return c;
}
int main()
{
std::vector<int> a( { '1', '2', '3' } );
std::vector<int> b( { '1', '9' } );
for ( int x : a ) std::cout << static_cast<char>( x ) << ' ';
std::cout << std::endl;
std::cout << "+" << std::endl;
for ( int x : b ) std::cout << static_cast<char>( x ) << ' ';
std::cout << std::endl;
std::cout << "=" << std::endl;
std::vector<int> c;
c = a + b;
for ( int x : c ) std::cout << static_cast<char>( x ) << ' ';
std::cout << std::endl;
}
程序输出为
1 2 3
+
1 9
=
2 1 4
如果矢量包含整数值而不是字符,则代码会更简单。
或者向量可以声明为具有类型std::vector<char>
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 如何在for循环中包含两个索引值的测试条件
- 在声明中合并两个常量"std::set"(不是在运行时)
- 如何使用OpenMP并行这两个循环