简体   繁体   中英

Adding elements to an array in C++

My code takes input(str of ints essentially)

  input: 123 456 789 90  

I'm trying to take each number such as 123 456 etc. and adding them into one element of my array but I'm not sure how to add the entire 123 .

some_int represents the number of ints. So in this case, it would be 4   

int* arr_func(int some_int)
{
  std::string str;
  std::getline (std::cin, str);
  int* p = new arr[some_int];
  for (i=0;i<str.length(),++i)
  { 
  if (str[i] != ' ')
    {
    arr[n] = str[i];
    }
  }
  return p;
}    

I know what my code does and it's incorrect, but that's all I have right now. I want my array to look something like this at the end when I return it.

[123] [456] [789] [90] each [] denoting an element/cell.    

I know how to convert str to int, but the main focus is instead of just each cell/element being [1] [2] [3] [4] [5] , how do I make it so it appears like above. Thanks

您需要使用atoi()方法将char数组(字符串)转换为int。

arr[n] = atoi(string[i])

I dont like to give you the complete answer, but I'll show you how to solve this issue.

First you need to split your input with spaces that you can learn here .

You also need to create your array dynamic some thing like this :

int* input = new int[some_int];

After that use a for loop like

for(int i = 0 ; i<some_int ; i++){
    //some code here
}

And in above commented area use atoi() function to convert char* to integer that you can see the sample here .

I'd simply use std::cin instead of std::getline .

Also, AFAIK, when you allocace the memory, you should free it;
returning pointer and leaving responsibility for freeing it to the caller it's not the best idea,
because it's easy to forget about freeing the memory, which lead to memory leak.

I'd use std::vector instead. Alternatively, you can use some kind of smart pointer, but I won't tell you how to use it, because simply I don't know.

I'd write:

#include <iostream>
#include <vector>

std::vector<int> arr_func(const unsigned length) {
    std::vector<int> result;
    result.reserve(length); //optimization, can be skipped
    for(unsigned i=0; i<length; ++i) {
        int temp;
        std::cin >> temp;
        result.push_back(temp);
    }
    return result;
}

/* Other solution, may be less efficent */

std::vector<int> arr_func(const unsigned length) {
    std::vector result(length);
    for(auto& i:result)
        std::cin >> i;
    return result;
}

You can try something like this.Use the string stream to acheive this.Split the string into integers

#include<sstream>
#include <iostream>
using namespace std;

int main() {
    stringstream stream;
    string str="123 456 789";
    stream <<str;
    int n[3];
    for(int i=0;i<2;i++){
    stream >> n[i];}
    cout<<n[0]<<endl<<n[1]<<endl<<n[2];
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM