There are two strings with dynamic size. It's needed to write a function which returns true if these strings have at least one same character and false if they don't. Maximum size and strings should be inputted from file. <cstring> can't be used.
I wrote something but is's such a mess cause bool function returns wrong value and there's an error "33 22 [Error] no matching function for call to 'getline(std::ifstream&, std::vector<std::basic_string<char> >&)''. I'd be so greatful if you'll help me fix it
thats ok. messy wrong code where you tried is much more awesome than 'trust me, I tried, now write it for me'.
hang on!
line 33 needs to getline into a string, not a vector of string.
eg
string line;
getline(file, line);
str1.push_back(line);
if you had preallocated the vector, you can also fill it:
getline(file, str1[index]); //but [index] has to exist.
later, you want to try to preallocate vectors, but for now, don't stress over it if its a new idea.
don't mix >> << stream operations and getline. If you do that you need an ignore() but here, you don't use the size, maybe you can read that as a line/string and throw it away? It seems to serve no purpose.
Line 12: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1 2 3
while (stream >> var) // or while (getline(stream,var))
{ // Good operation
}
but what, exactly, are you trying to achieve? You mention strings that have at least one same character - but have a function to check for same string with vector??
but being stated that you can't use <cstring> and having line size specified in the file sort of suggests you are expected to use c-style strings (not std::string or std::string_view) - and that you are expected to 'roll your own' strcspn() ????
would putting the letters from each into <set>s and then doing an intersection be as fast as the counting sort approach...? It would be minimal code. and it would work in unicode.
.... I always forget how messed up <set> is.
but that idea, using more useful containers, is only:
which means that if result[0] isnt zero, they share a letter if you want the bool response.
Given that you have to sort the strings, its probably not the fastest possible. But its simple enough -- the actual bool result is obtainable from only lines 5,6,7 given that your variables and my printer loop don't matter.
Both I and JLBorges already gave responses on OPs duplicate thread using sets. You will not get better performance than that. No need to perform any sorting.
1 2 3 4 5 6 7 8 9
#include <unordered_set>
bool has_common_codepoints( const std::string& a, const std::string& b )
{
std::unordered_set <char> cs;
for (auto c : a) cs.insert( c );
for (auto c : b) if (cs.count( c )) returntrue;
returnfalse;
}
I saw. I was just trying to play with the intersection idea. I rarely use those and forgot that it was about useless -- you would think <set> would just have an intersect with another set that was efficient, but ... swing & a miss.
Sorry if have the wrong end of the stick, but isn't R in the first code snippet an xvalue? Would one need to return the type Set& instead? Or make it an out parameter?