New answers tagged

0

Always check input succeeded. std::cin >> n; std::cin >> arr[i]; In both these lines, we ignore all errors, and will produce the wrong output without any warnings, and happily return 0 from main(). This is bad for any program used as a processing step (e.g. driven by Make).


2

Something to keep in mind is that this statement is truncating data: arr[j]=av; because it is assigning a double to an int. As was mentioned in a comment there are c++ container types that would be better than a old C style array, two of these are std::array<type, arraySize> and std::vector<type>. std::array is a fixed size and std::...


6

Code review in order of appearance: public static String getAllExecTimes(MasterVO masterVO, boolean displayAll) { Static methods should be avoided if you may expect them to contain state, such as hints on how to format the class. Why not create an object that performs the formatting? Boolean parameters are not a good idea. They are pretty unreadable. But ...


4

Classes IgnoreUntilNewLine and SkipEmptyLineToEnd can probably be simplified. Classic indicators for that are that it only has one function and no state. However, the key is the following operator>> overload basic_istream& operator>>( basic_istream& (*func)(basic_istream&) ); In other words, you read into a function. This is used by ...


4

IgnoreUntilNewLine is too long in my opinion. I'd simplify it to ignore_line. It probably makes sense to provide the manipulator object in the library instead of letting the user declare one: struct ignore_line_t { constexpr explicit ignore_until_newline_t() = default; // ... }; inline constexpr ignore_line_t ignore_line{}; so the user can use it ...


2

Your class is InterviewQuestion, if it is for an interview, then there's a few other things to consider, in addition to whether or not you get a reasonably efficient solution. Whilst a bad solution will probably rule you out, a non-optimal solution may be acceptable depending on other factors. Seperation of concerns Finding the list of unique product ...


0

The exact time complexity of your algorithm is O(2*n) since it performs two sequential iterations on the array. I do have a slightly better performing algorithm: Sort the array by the first item in the 2nd dimension array (item number) iterate over the sorted array looking for items that have exactly one line that has the requested target seller. time ...


5

@Joop Eggen has already made some good suggestions based around your original code. You've made some slight modifications in response to comments to the parameters of your function, however your code still doesn't really make sense in some ways. for (int i = 0; i < 100; i++) { It's unclear why you'd want to perform this operation 100 times. If it ...


5

Probably not the right forum. In java f(x) the method f will never change the variable x to another value; it cannot be make it null or true or whatever. (A rule to prevent a category of errors.) So you want a result: public static class Decision { String destination; boolean conditionFound; } As in java null's should in normal code not be valid, ...


3

String parsing You are right that C++ slightly lacks some convenient features for string parsing. If you were not trying to count lines as well as find words, then a single while loop with: inFile >> s; would have sufficed, because streaming will terminate on whitespace by default. But because you want to count lines, you need the 2 loops and ...


3

I was somewhat curious if Woodstox has improved, so I wrote a complete parser for your example data. It's in a different style than your code, complete repo: https://github.com/chhh/testing-woodstox-xml-parsing My results with fake data records that I created: Parsed 4,000,000 persons (1.36 GB) in 16.75 seconds (Ryzen5 3600), memory usage wasn't really ...


4

I have some suggestion for the code, that will not make to code faster, but cleaner, in my opinion. You can use the try-with-resources to handle the closing of the stream automatically (java 8+) try(BufferedWriter br = new BufferedWriter(new FileWriter(file + ".csv", true), 64 * 1024 * 1024)) { //[...] } I suggest that you separate the code in more ...


3

I was in a similar position several years ago - needing to parse multi-gigabyte XML files. Tried all the standard solutions Woodstox, Xerces, Piccolo whatnot - can't remember all the names. Ended up using an XML parser from a library called Javolution. It's development has stalled a while back, but the parser works well. Available from Maven Central: https://...


Top 50 recent answers are included