#question regarding strings as inputs and parsing

68 messages · Page 1 of 1 (latest)

urban quiver
#

Hey so I just finished a leetcode type question (https://dmoj.ca/problem/lkp18c2p1 if you want to take a look). The question regarded taking 2 lines of input from user, ex:

3 4
3 2 5

In my answer, since I ever only really used the getline function to put input strings into variables, then stoi(variable) to make it an integer, I was just wondering if there was a different way of doing this (with less code). In the book that i am going through, the author writes his answer in C, and uses the scanf function, but ever since starting c++ ive never seen that

fiery tokenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

urban quiver
#
#include <iostream>
#include <string>
#include <vector>
//input 3  4 
//      3  2  5

//output
//      2
//      3
//      3
//      4

int find_shortest(int lines[], int n)
{
    int shortest = 0;
    for (int i = 1; i < n; i++)
    {
        if (lines[i] < lines[shortest])
            shortest = i;
    }
    return shortest; //returns smallest index with smallest people in line (smallest integer)
}

void solve(int lines[], int n, int m)
{
    int shortest;
    for (int i = 0; i < m; i++)
    {
        shortest = find_shortest(lines, n);
        std::cout << lines[shortest] << "\n";
        lines[shortest]++;
    }
}



int main()
{
    // int test_lines[] = {3, 2, 5};
    // solve(test_lines, 3, 4);
    std::string sn, sm;
    int n, m;
    getline(std::cin, sn, ' ');
    getline(std::cin, sm);
    n = std::stoi(sn);
    m = std::stoi(sm);
    int lines[100];
    std::string scurrent;
    for (int i = 0; i < n; i++)
    {
        if (i != n - 1)
        {
            getline(std::cin, scurrent, ' ');
            lines[i] = stoi(scurrent);
        }
        else{
            getline(std::cin, scurrent);
            lines[i] = stoi(scurrent);
        }
    }

    solve(lines, n, m);
    return 0;
}
#

my answer if anyone is interested^

wraith quartz
#

Why not use std::cin for string sn, since the delimiter is a space anyways

#

If you use cin, you'll have to use cin.ignore(numeric_limits<streamsize>::max(), '\n') but that should be fine

rocky sparrow
#

same with scurrent

#

inside if(i != n-1)

#

why u even need if statment her?

#

just put else outside the loop

#

without else of course

wraith quartz
#

The description lol

wraith quartz
rocky sparrow
#

second it will still have less code...

wraith quartz
#

You mean like this or something different?

 for (int i = 0; i < n; i++)
    {
       getline(std::cin, scurrent, ' ');
       lines[i] = stoi(scurrent);
    }
getline(std::cin, scurrent);
lines[n-1] = stoi(scurrent);
    }
rocky sparrow
#
for(int i = 0; i < n-1; i++)
{
  std::cin >> scurrent;
  lines[i] = stoi(scurrent);
}
getline(std::cin, scurrent);
lines[n-1] = stoi(scurrent);
rocky sparrow
#

and n-1 in check

#

i will try make this code better

urban quiver
urban quiver
wraith quartz
# rocky sparrow and n-1 in check

If you want to make the code shorter, it's a good way of doing it but the previous code while being a little longer is more clear, so I would personally prefer that

rocky sparrow
#

btw @urban quiver do u allow to use std::array?

#

because most code look like c

#

i mean c style

urban quiver
#

like just initializing array

#

like int array[];

rocky sparrow
#

std::array<type, size>

urban quiver
#

ohhh idk ive never used that you probably can

rocky sparrow
#

use it

#

it just better

urban quiver
#

isnt vector just better in all cases tho

#

std::vector<type>

rocky sparrow
#

no

#

if u need array

urban quiver
#

ohh well

#

if i alr know the array length

#

yea i understand

rocky sparrow
#

u can get size

urban quiver
#

oh like .size()

rocky sparrow
#

and iterators work too

urban quiver
#

yea i seen that i couldnt do .size() or .length() for regularly initialized array

rocky sparrow
#

which u can use with raw array but

urban quiver
#

do i have to #include <array>

rocky sparrow
#

yup

urban quiver
#

ahh ok bet

#

so is that what people mean when they say c-style array

#

its the std::array

rocky sparrow
#

yup

#

i mean it just raw array

urban quiver
#

and regular array that is like int array[];

#

thats just stupid array

wraith quartz
# urban quiver like int array[];

That's a C-style array. std::array can be passed to or returned from functions by value, its interface also makes it more convenient to find the size

urban quiver
#

ok i understand i think

rocky sparrow
#

and now u can use std::min_element for find shortest

wraith quartz
#

;compile

#include <iostream>
#include <string>
#include <vector>
#include <array>

void add(std::array<int, 100> &b){
    int size = b.size();
    std::cout << size;
}

int main()
{
   std::array<int, 100> a = {1,2,3,4,5};
   add (a);
}
stoic spadeBOT
#
Program Output
100
wraith quartz
#

If you use std::array, you don't have to pass in int n which represents the size to solve and find_shortest functions

rocky sparrow
#
template<size_t s = 0>
int find_shortest(const std::array<int, s>& lines)
{
    return *std::min_element(lines.cbegin(), lines.cend());
}
#

or just u can remove fully this function

#
void solve(std::array<int, s>& lines, int m)
{
    int shortest;
    for (int i = 0; i < m; i++)
    {
    shortest = *std::min_element(lines.cbegin(), lines.cend());
        std::cout << lines[shortest]++ << "\n";
    }
}
rocky sparrow
#

@urban quiver btw better to use iftream if u have test values already

#

because then u not need input anything

#

u just run and check result