I have the following program which takes in an array form such as: [12,34,55,6]
I am using a for and while loops to read character by character and if a number is read it is store in the vector SumVec
, the problem I am facing is that the vector stores only one number at a time is there any way to fix this.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
vector <int> SumVec;
string Nums;
int ReadNums(__int32* Array);
__int32 ArraySize;
};
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
int Solution::ReadNums(__int32* Array){
ArraySize = 0;
Array = new int[ArraySize];
for (int i = 0; i < Nums.size(); i) {
if (Nums[i] == '[' || Nums[i] == ']'|| Nums[i] == ',' || Nums[i] == ' '); //do nothing
else {
int j = i;
while (1) {
j;
if (Nums[j] != '[' && Nums[j] != ']' && Nums[j] != ',') {
SumVec.push_back(Nums[j]);
}
else
SumVec.push_back(Nums[i]);
}
}
}
return ArraySize;
}
int main()
{
std::cout << "Welcome to the Two Sum!\n";
Solution Soln;
cout << "nums = "; //[12,34,55,6]
cin>>Soln.Nums ;
__int32* ArrayRequst = new __int32[Soln.ArraySize];
Soln.ReadNums(ArrayRequst);
for (auto i = Soln.SumVec.begin();i!= Soln.SumVec.end(); i){
cout << *i << endl;
}
}
Current INPUT: [12,34,55,6]
Current OUTPUT:
1
2
3
4
5
5
6
CodePudding user response:
I have made some changes and written comments throughout the code
#include <iostream>
#include <vector>
#include <cctype>
#include <string>
using namespace std;
// first of all, using namespace std is generally not a great idea.
// Because if it's in a header file, you can force it upon others(but it's not a problem here)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
vector <int> SumVec;
string Nums;
// changed all the __int32 to normal int's, it just makes the code incompatible in my opinion
int ReadNums();
//int ArraySize; this is not necessary since we can just do SumVec.size()
};
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
int Solution::ReadNums(){
for (int i = 0; i < Nums.size(); i) {
if (Nums[i] == '[') continue;
// if (Nums[i] == ']'|| Nums[i] == ',' || Nums[i] == ' ');
//do nothing, is a bit weird, just test for the other case (isdigit), or negate the result
if (std::isdigit(Nums[i])) {
int j = i;
string buffer;
while (true) {
if (Nums[j] == '[' || Nums[j] == ']' || Nums[j] == ','){
SumVec.push_back(std::stoi(buffer));
break;
}
// as long as we are reading digits, append them to a buffer
else if (std::isdigit(Nums[j])){
buffer = Nums[j];
i;
// increment the i value, we dont want to loop twice over the same digit
}
j;
}
}
}
return SumVec.size(); // as said not necassary
}
int main()
{
std::cout << "Welcome to the Two Sum!\n";
Solution Soln;
cout << "nums = "; //[12,34,55,6]
cin>>Soln.Nums ;
Soln.ReadNums();
for (auto i: Soln.SumVec){
cout << i << endl;
}
}
lastly i dont understand why you used __int32* ArrayRequst = new __int32[Soln.ArraySize];
so i just ignored it.
CodePudding user response:
You could do this instead:
- Parse your input string using a string stream, and
- read your numbers directly into a vector of
int
s.
#include <iostream> // cout
#include <sstream> // stringstream
#include <string>
#include <vector>
int main()
{
const std::string line{"[12,34,55,6]"};
std::stringstream ss{line};
char c{};
ss >> c; // read '['
std::vector<int> nums{};
int num{};
while (ss >> num >> c) // read <number>',' or <number>']'
{
nums.push_back(num);
}
for (auto& num : nums)
{
std::cout << num << "\n";
}
}
// Outputs:
// 12
// 34
// 55
// 6