Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have implemented a multi-map, but I was wondering how do I access the first 5 elements of a multimap?
I tried using a for-loop, but that didn't work out. Any suggestions?
Although it would be easier to help you if you were to post a minimal reproducible example, as text, formatted as a code sample, I think I still understand what you’re asking.
This looks like a learning exercise that you want to solve on your own. But I can give some advice.
What you want to do is check two conditions: that you’ve read five elements, or that you’ve run out of them. Declare both a loop counter initialized to 0 and an iterator.initialized to .begin(). Loop until the counter is equal to 5 or the iterator is equal to .end(). On each iteration, increment both the counter and the iterator. You might express this as a while loop, but you could also do it with comma operators in your for loop.
Also, please indent your code properly and use braces under your for and if statements. It’ll save you from writing a lot of bugs and make your code much easier to read.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
I am using XMPie and need a regex to character count for me. My Regex is ^[\S\s]{1,800}$ but I am getting a different result with the refresh preview button than the next screen button. I had a support case on it and it went all the way to R&D and they basically said, there is nothing we can do, try a different expression. I think one button must be implemented with C# and the other with JS or some such thing. What seems to be happening is that one is counting a return once (refresh), while the other is counting 2 characters for it. I think it must see it as \r or \n and count that as 2. Any ideas on how I could modify my expression to prevent that? I mostly just want consistency, so making it explicitly count it twice, or not count it at all would be more livable than differing results, especially as refresh is lower.
Thanks so much for any help!
I have tried a slew of different expressions, but I am not great with regex and nothing has given me a better result.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I came across 2 codes depicting the use of std::vector::resize() and std::vector::shrink_to_fit() and could not understand which one demolishes the elements of the vector.
Example 1
Example 2
In example-1 vector has all the elements even after resize(5) is used while in example-2 resize(4) eliminates the 5th element of the vector. Have a look and tell if I'm getting something wrong.
That first example is undefined behavior as the vector only has 5 elements after resize(5) is called and elements up to index 9 are accessed. However, its likely to work in a release build as the memory for the rest of the elements haven't been freed yet. A debug build will probably catch the error though.
shrink_to_fit() won't change the contents of the vector. However, it might move the elements to a smaller piece of memory and free the old memory causing the previous bug to show.
The second example uses the vector correctly.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create a string array to hold the strings generated by a void function. I am very confused about if or how I can do this. The below code is giving me an error about "no suitable constructor."
getWords("theFile.dat"); // Function to extract list of strings from file
string wordsList[] = {getWords("theFile.dat")}; // Add strings from function to array
If (as your comment states) the code uses cout to print the strings, then you have no way to capture them and put them in your array. Well, ok, you might be able to, by doing some major/ugly hack that grabs hold of the stdout file descriptor and reads what is written to it, but that would just be ugly beyond words (and most likely would need different platform specific implementations). Just don't go there.
Write your own good function to read the file and return what you need or fix the crap function you've been given.
There's got to be a limit to how much crud (with the risk of introducing more crap/bugs) one has to add to work around broken crap code before fixing or re-implementing/re-factoring it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm stuck at this problem where I have to run some tests and output the result. It's nothing special but the problem is it says "several test cases".
I mean it doesn't take any variable to fix the number of test cases. I wrote the code for one case and can run it a fixed amount of time with a loop. But how do I know when to stop the code when i don't know how many test cases the online judge is going to run?
There must be some input that starts a set of input. For example an array length/ number of elements or a string. On that thing you have to keep an eye. Just keep a loop like this..
int n;
while(cin>>n)
{
//do something
}
If nothing is mentioned you need not consider this.
In competitive programming in general the number of test cases are mentioned..so then you can just loop over a variable considering the testcase numbers.
Another way to specify end of inputs
There is another way of specifying end of input (Often encountered in UVA online judge ) that is in the last line they give -1 or a string "END". So in those cases you may take values into variables after completing a test case and check whether -1 or "END" is encountered and exit as required.
Example
For example: this problem [From Codeforces] specifies no test cases. Here it is expected that you just get n and the elements and process it and give output. Nothing else. You don't have to consider the testcases as it is not mentioned.Your program will be run multiple times on different input sets.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am relatively new to programming, and I can only really understand C++. I have recently started working on a project that requires the user to input something that will allow them to make a selection. I can't figure out how to make it possible for the user to input a string or a char but get the same result. I know that this would require that I assign the variable that the user inputs (for example 'a') two data types, but how do I do that? I've tried using "string/char a;" but that doesn't work.
Could someone please help me with multi-data-type variables?
Thanks
The string type will work for all user input. Since it "doesn't work" for you, we can't help you further if you don't show us what you tried.
If the User is the one making the Input from the I/O, then you get to decide if you will treat the input as a string or char. After receiving the Input you should know what you want to do with it. And you can also store the input data in array, vectors or list. Primitive data types can do so many things just understand the purpose and function of your program.