我有以下采用陣列形式的程式,例如:[12,34,55,6]我正在使用 for 和 while 回圈逐個字符讀取,如果讀取了一個數字,它將存盤在向量中SumVec,我面臨的問題是向量存盤一次只有一個數字有什么辦法可以解決這個問題。
#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;
}
}
當前輸入:[12,34,55,6]
當前輸出:
1
2
3
4
5
5
6
uj5u.com熱心網友回復:
你可以這樣做:
- 使用字串流決議您的輸入字串,然后
- 將您的數字直接讀入
ints的向量中。
[演示]
#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
uj5u.com熱心網友回復:
我在整個代碼中進行了一些更改并寫了注釋
#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;
}
}
最后我不明白你為什么使用 __int32* ArrayRequst = new __int32[Soln.ArraySize];所以我只是忽略了它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405196.html
標籤:
上一篇:為什么用戶定義型別別的靜態和區域變數的合成默認建構式的行為不同?
下一篇:泛型:如何從陣列中填充地圖?
