Does swap() function works in iostream header? - c++

#include <iostream>
using namespace std;
int main(){
swap(a[i],a[j])
}
In this the swap function work automatically or I need to create a another swap function to solve this?

swap is not a keyword in C++ but rather a function from the C++ standard library. So to use it you need to bring in the function from the appropriate C++ standard library header. Unfortunately std::swap has been rather itinerant since it first made it into the C++ standard:
Up to but not including C++11, you need to #include <algorithm>
From C++11 you need to #include <utility>
From C++17 you can #include <string_view> instead
Relying on the implicit inclusion of the header means you're not writing portable C++.

Related

Why are these C++ headers specified to include <initializer_list>?

As can be seen from https://stackoverflow.com/a/26614984/481267 the following headers are guaranteed by the standard to #include <initializer_list>:
Everything in [containers]
<utility>
<string>
<algorithm>
<random>
<valarray>
<regex>
Most of these headers declare at least one function that takes a std::initializer_list<E> argument, so it makes sense. However,
<array>, <stack>, and <queue> have no such functions, although perhaps it makes sense to treat all containers uniformly here.
<utility> has no such functions.
<iterator> does have functions with an initializer_list argument (rbegin, rend) but it's not specified to include <initializer_list>.
What is the rationale behind these exceptions?
Seems like there is no explicit rationale, just that some proposals for additions to the standard were made and those proposals were accepted.
At the very end of the document N2672 Initializer List proposed wording it just says:
In 20.2 Utility components [utility] paragraph 1:
This subclause contains some basic function and class templates that are used throughout the rest of the library.
Header <utility> synopsis
#include<initializer_list>
namespace std {
So, the authors of the paper saw initializer_list as a utility, and so it ought to be included with the <utility> header. And therefore it is.
The paper didn't propose any changes to the <iterator> header, so none were made.

c++ compiler choosing wrong namespace

I am using the OpenCV library for my project and using namespace "cv" and "std" for my source files.I want to use the string split function to split a string based on a delimiter .But the "split" function is defaulting to the namespace "cv" and is showing errors related to wrong arguments as it is expecting arguments related to the OpenCV slpit function.
What can be done to overcome this problem?I see that the same function worked fine in another source file even though that one has got both std and cv anmespace.
These are the headers I am including in both files -
#include "stdafx.h"
#include <unordered_map>
#include <iostream> // for standard I/O
#include <fstream>
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
Avoid putting using namespace std; and using namespace cv; in your code, especially in headers.
If you can't stop yourself, fully qualify the function where you need to, e.g.
std::split(...)
As long as the argument sets for the cv and std functions are disjoint, you shouldn't introduce any ambiguity in the overload resolution (both would be injected into the global namespace).
Based on a comment showing split is from a 2013 proposal much more likely is that your compiler just doesn't implement std::split yet.
All you need to do is prefix the function with its' namespace
For instance
std::split(...);
You can overcome this by using explicit namespace specifiers.
std::split

c++: create a insert_iterator without #include <iterator>

In the code below I create an insert_iterator without including the iterator header.
How is possible that this compiles? I'm new to c/c++ so this will likely have an obious explanation.
#include <list>
//#include <iterator>
int main()
{
std::list<int> mylist(10,1);
std::insert_iterator<std::list<int> > it(mylist,mylist.begin());
return(0);
}
It happens to compile because some of the other headers, <list> in your case, is pulling <iterator> as well. This may work fine with one implementation but fail on a different one, or even on the next version of your current library implementation.
You should always include the headers that define the stuff you use, to make sure it will compile everywhere. Note that there are some guarantees of standard headers that are pulled by other standard headers, but I don't think this is one of those exceptions.

<string.h> or <string>?

Which is the best way to include the standard header string.h in a C++ project?
Using the [dot]h at the end, like this:
#include <string.h>
or just writing
#include <string>
Or, maybe, using another way that I don't know?
Thanks!
Those are two different headers.
<string> is for c++ std::string class
<string.h> is for c string functions (like strlen(), etc.), which should be <cstring> for c++ project (this is the third, you didn't know of).
its quite different!
<string.h> this library for C-style strings
<string> for C++ strings
by standard in C++ you should use <cstring> instead <string.h>
Wiki says:
The C++ Standard Library also incorporates 18 headers of the ISO C90 C
standard library ending with ".h", but their use is deprecated. All
other headers in the C++ Standard Library DO NOT end in ".h".
Each header from the C Standard Library is included in the C++
Standard Library under a different name, generated by removing the .h,
and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
string is c++ stl headfile
provide the template class ‘string’
string.h is c standard headfile
provide many function to use. like strlen strcpy memcpy.
if you want use in namespace std,which is not use globe namespace or not want to use string.h
you can use cstring instead.
The *.h headers files are often C header files, that you can use in C++ perhaps with extern "C" { ... } wrapping
The headers without any *.h are usually genuine C++ headers.
It is a rule of thumb only.
The latest and previous C++ standards (c++11, C++03) define headers like <cstdio> to wrap properly the original C headers, using namespaces, etc.
The standard is
#include <string>

C++ error: ‘string’ has not been declared

In my header file I'm getting the
error: ‘string’ has not been declared
error but at the top of the file I have #include <string>, so how can I be getting this error?
string resides in the std namespace, you have to use std::string or introduce it into the scope via using directives or using declarations.
Use
std::string var;
or
using namespace std;
string var;
String is in a std namespace so you must let your compiler know.
Most of the classes and class templates, and instances of those classes, defined by the Standard Library (like string and cout) are declared in the std:: namespace, not in the global namespace. So, whenever you want to use (say) string, the class is actually std::string.
You can add that namespace prefix explicitly every time you use such a class, but that can become tedious. In order to help, you can add using declarations for those classes you use frequently.
So, in your case, you could add lines like the following, generally somewhere shortly after the corresponding #include lines:
#include <string>
#include <iostream>
using std::string;
using std::cout; // Added as illustrative examples of other
using std::endl; // ... elements of the Standard Library
Or, if you have a compiler that supports the C++17 Standard (or later), you can put more than one class in the same statement:
using std::string, std::cout, std::endl; // C++17 or later
But, beware of using the generic, "cover-all" using namespace std; directive (even though that is actually valid C++ syntax). See: Why is "using namespace std;" considered bad practice?
For a more general introduction to, and overview of, the Standard Template Library (now, more correctly known as the C++ Standard Library), see the tag-Wiki for the stl tag.