【问题标题】:How to add two ints next to make one int如何在旁边添加两个ints以制作一个int
【发布时间】:2022-01-28 03:28:15
【问题描述】:

我有以下程序,它采用数组形式,例如:[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

【问题讨论】:

  • 您需要从子字符串中解析数字,您的代码只检查单个字符
  • 不要使用所谓的“竞赛”或“在线评委”网站来学习编程。您在代码中犯了几个初学者错误,通过阅读some good books 并参加课程以正确学习,可以轻松避免这些错误。
  • @Omar 那么他们给了你不好的建议!显示的代码中存在太多问题,并不是一两件简单的事情会“神奇地”解决问题。很抱歉,您对 C++ 的了解是主要问题之一。 C++ 不能靠猜来教,你真的需要从基本的“hello world”程序开始学习,并一步一步地正确学习。
  • @Someprogrammerdude cout
  • 代码中的几个问题:Array 是做什么用的?为什么不释放ReadNums 中分配的内存?为什么允许用户未经检查地访问ArraySize?如果用户输入负数会发生什么?为什么使用__int32 而不是size_tint32_t(非编译器特定且在标准中可用)?你如何退出while(1) 循环?为什么要使用数组,当您有可用的 std::vector 来承担内存管理的负担?

标签: c++ c++11


【解决方案1】:

你可以这样做:

  • 使用字符串流解析您的输入字符串,然后
  • 将您的数字直接读入ints 的向量中。

[Demo]

#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

【讨论】:

    【解决方案2】:

    我在整个代码中进行了一些更改并编写了 cmets

    #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]; 所以我忽略了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多