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 8 years ago.
Improve this question
I have map<string, inode_ptr> directories
I want to retrieve the value associated with a certain key.
so I did this: inode_ptr i = directories.find("string");
But it is returning the string key, what should I do?
Use the following approach
inode_ptr i = NULL;
auto it = directories.find("string");
if ( it != directories.end() ) i = it->second;
Maybe it is even better to write
inode_ptr i = {};
instead of
inode_ptr i = NULL;
Give this a go:
inode_ptr myInode = NULL;
map<string, inode_ptr>::iterator i = directories.find("string");
if ( i != directories.end() )
{
myInode = i->second;
}
else
{
cerr << "no 'String' in the map, I should be "
<< "sure to check for null before using myInode" << endl;
}
Here is a link to that fine manual referenced in the above comments:
http://www.cplusplus.com/reference/map/map/find/
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 2 years ago.
Improve this question
string longestCommonPrefix(vector<string>& strs) {
string res = "";
int i, j;
bool flag = true;
for(int i=0; i<strs[0].size(); i++)
{
for(int j=0; j<strs.size()-1; j++)
{
if(strs[j][i] == strs[j+1][i])
{
flag = true;
}
else
return res;
}
if(flag == true)
{
res += strs[0][i];
}
}
return res;
}
I was doing this leetcode question where we had to find the longest common prefix of given array of strings and then i got stuck at this i cant understand what is the meaning of this error, most of the test cases are passed so i don't think logic is wrong.Is there any corner cases i am missing?
Runtime Error Message:
Line 924: Char 9: runtime error: reference binding to null pointer of type 'std::__cxx11::basic_string, std::allocator >' (stl_vector.h)
Last executed input:
[]
Thanks in advance
Its null pointer exception. So you should check if str is null i.e. str=='" for each string in vector.
and return answer accordingly.
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 don't understand how to iterate only on a portion of an Stl map and not from begin to end like in standard traversate. Here is my code:
auto end = temp_map.rbegin() + THRESHOLD_NUM;
for (auto rit = temp_map.rbegin(); rit != end; ++rit)
{
int s = rit->second;
for (int k = 0; k < MAX_ROWS; k++)
{
array_dist_it[k] = abs(input[k] - input_matrix[k][s]);
}
float av_real = mean(MAX_ROWS, array_dist_it);
float score_real = score_func(av_real);
rank_function(score_real, s);
}
}
I think that the problem is related to the syntax of the for loop and in particular to the iterator. The error is about an invalid operator.
A std::map has a BidirectionalIterator. It supports incrementing and decrementing but not addition or subtraction. If you need to advance and iterator N times then you can use std::next. Using that instead of
auto end = temp_map.rbegin() + THRESHOLD_NUM;
You would have
auto end = std::next(temp_map.rbegin(), THRESHOLD_NUM);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can I use null or nullptr as a bool value instead of 0/false?
Since declaring a bool as null means that the bool value is actually zero/false and that does not work in my case. I am using zero/false as another possible outcome. I would like to have a bool value that can be 'non existent' (neither true nor false).
You could take a look at boost::optional or boost::tribool
If you can use boost in your project, then boost::optional is for you. It works like Haskell's Maybe if you're familiar with that.
Simple examples of use (from http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/boost_optional/examples.html):
// return values
optional<char> get_async_input()
{
if ( !queue.empty() )
return optional<char>(queue.top());
else return optional<char>(); // uninitialized
}
void receive_async_message()
{
optional<char> rcv ;
// The safe boolean conversion from 'rcv' is used here.
while ( (rcv = get_async_input()) && !timeout() )
output(*rcv);
}
// local variables
optional<string> name ;
if ( database.open() )
{
name = database.lookup(employer_name) ;
}
else
{
if ( can_ask_user )
name = user.ask(employer_name) ;
}
if ( name )
print(*name);
else print("employer's name not found!");
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 years ago.
Improve this question
I Keep getting this error when I try to set the first char in firstname and lastname to a null and return it to a 1.
my code is...
int DeregisterStudent(int SID, struct studentdata SRecord[])
{
int index;
index = SRecordSearch(SID, MAXRECS, SRecord);
if(index >= 0)
{
SRecord[index].sid = 0;
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
return 1;
}
return 0;
}
and the error I get is error:incompatible type in assignment
for these two lines
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
You are not writing to the string correctly, it should be
SRecord[index].lastname[0] = '\0';
or
SRecord[index].lastname = "";
depending on how the struct was declared. In the second case you might be overwriting a dynamically allocated string pointer, in which case it should be free()ed.
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 8 years ago.
Improve this question
Is there another solution, please?
if(!http_piconpath && http_tpl)
{ http_piconpath = http_tpl; }
If not exist http_piconpath but exist http_tpl then assign the value from http_tpl to http_piconpath.
You provide very little information about what you are doing. Assuming that you use strings from your comment, the if statement you've got is not valid for strings, you'll see your compiler screaming that it can't convert string to bool. Below is a very basic example. Note that you must initialise http_piconpath, else it will have a garbage value and you won't know if its value is set or not.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string http_piconpath = "";
string http_tpl = "string";
if(http_piconpath == "" && http_tpl != "") {
http_piconpath = http_tpl;
}
cout << http_piconpath << endl;
return 0;
}
Supposing that both are pointers (of compatible types),
if(!http_piconpath) http_piconpath = http_tpl;
Or
http_piconpath = http_piconpath ? http_piconpath : http_tpl;
If picon is null, it gets the value of tpl; if both are null, nothing changes.