error: no type named 'cout' in namespace 'std'

I must have introduced some error that I can't pinpoint

been writing

using std::cout;

instead of using namespace;

but somehow i can't compile this and I keep getting

error: no type named 'cout' in namespace 'std'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <unordered_set>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::unordered_set;
using std::vector;


class Solution {
public:
    void printDuplicate (int arr[], int n) {
        unordered_set<int> intSet;
        unordered_set<int> duplicate;

        for(int i=0; i<n; i++){
            if (intSet.find(arr[i]) == intSet.end())
                intSet.insert(arr[i]);
            else
                duplicate.insert(arr[i]);
         }

    }
    cout << "Duplicate items are: ";
    unordered_set<int> :: iterator itr;

    for(itr = duplicate.begin(); itr != duplicate.end(); itr++)
        cout << *itr << " ";
    };



int main(){
    int arr[] = { 1,2,3,5,6,7,9,3,6};

    Solution test;

    int n = sizeof(arr) / sizeof(int);

    test.printDuplicate(arr, n);

    return 0;
}
Last edited on
and if i write the code as follows, there's no compilation error.

i'm trying to figure out what I did wrongly with the code in the first post.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <unordered_set>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::unordered_set;
using std::vector;

// Print duplicates in arr[0..n-1] using unordered_set
void printDuplicates(int arr[], int n)
{
    // declaring unordered sets for checking and storing
    // duplicates
    unordered_set<int> intSet;
    unordered_set<int> duplicate;

    // looping through array elements
    for (int i = 0; i < n; i++)
    {
        // if element is not there then insert that
        if (intSet.find(arr[i]) == intSet.end())
            intSet.insert(arr[i]);

            // if element is already there then insert into
            // duplicate set
        else
            duplicate.insert(arr[i]);
    }

    // printing the result
    cout << "Duplicate items are : ";
    unordered_set<int> :: iterator itr;

    // iterator itr loops from begin() till end()
    for (itr = duplicate.begin(); itr != duplicate.end(); itr++)
        cout << *itr << " ";
}

// Driver code
int main()
{
    int arr[] = {1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5};
    int n = sizeof(arr) / sizeof(int);

    printDuplicates(arr, n);
    return 0;
}
In the OP, L26 & L29-31 are not within the function definition. Hence errors.

thanks seeplus, now I see my mistake.

much appreciated.
Hi,

It might seem easier to put using std::cout; et al at the beginning of your files, there will come a point when you get tired of having lots of these in a file. It is annoying if you give part of your code to others, they discover it doesn't compile because the offending using statements aren't there. Then one has to go through and fix these issues. It is better to do the following and not create issues.

It's probably better to just put std:: before each std thing. One gets used to it; it should also possible to setup keyboard shortcuts in your IDE. I gather these are the things that coding professionals do.

Edit

IIt should also be possible to setup the IDE editor to automatically do pairs of braces, parentheses etc. , so no more having code outside functions, classes etc.
Last edited on
There is a more efficient way to print duplicated numbers using a std::map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <map>
#include <chrono>

using std::cout;
using std::cin;
using std::endl;
using std::unordered_set;
using std::vector;

template<class TimeUnit = std::chrono::milliseconds>
class Timer {
public:
	Timer()
	{
		m_start = std::chrono::steady_clock::now();
	}
	~Timer()
	{
		std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now();
		std::cout << "** Running time: " << std::chrono::duration_cast<TimeUnit>(stop - m_start).count();
	}
private:
	std::chrono::steady_clock::time_point m_start;

};

// Print duplicates in arr[0..n-1] using unordered_set
void printDuplicates(int arr[], int n)
{
  // declaring unordered sets for checking and storing
  // duplicates
  unordered_set<int> intSet;
  unordered_set<int> duplicate;

  // looping through array elements
  for (int i = 0; i < n; i++) {
    // if element is not there then insert that
    if (intSet.find(arr[i]) == intSet.end())
      intSet.insert(arr[i]);

    // if element is already there then insert into
    // duplicate set
    else
      duplicate.insert(arr[i]);
  }

  // printing the result
  //cout << "Duplicate items are : ";
  unordered_set<int> ::iterator itr;

  // iterator itr loops from begin() till end()
  for (itr = duplicate.begin(); itr != duplicate.end(); itr++)
    cout << *itr << " ";
}
// Print duplicates in arr[0..n-1] using std::map
// based on some code from SO

void print_duplicates(int arr[], int n)
{
  std::map<int, size_t> cntMap;
  for (int i = 0; i < n; ++i) 
    cntMap[arr[i]]++;
  for (const auto&[first,second] : cntMap)
    //cout << first << "-> " << second << "\n";
    if (second > 1)
      cout << first << " ";
}

int main()
{
  int arr[] = { 1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5 };
  int n = sizeof(arr) / sizeof(int);
  {
    Timer<std::chrono::microseconds> t;
    printDuplicates(arr, n);
  }
  cout << " micros \n";
  {
    Timer<std::chrono::microseconds> t;
    print_duplicates(arr, n);
  }
  cout << " micros \n";
}


Output:
1 2 5 ** Running time: 755 micros
1 2 5 ** Running time: 298 micros
Press any key to close this window . . .
Last edited on
Well on my system I get (as x64):


1 2 5 ** Running time: 464 micros
1 2 5 ** Running time: 938 micros


.....
Very strange. I tried x64 on VS 2019 Release and the first version is even worse:
1 2 5 ** Running time: 1449 micros
1 2 5 ** Running time: 322 micros


Maybe later I have more time and can try https://quick-bench.com
Last edited on
I agree that from the code the second should be faster. Tried again:


1 2 5 ** Running time: 432 micros
1 2 5 ** Running time: 976 micros


maybe it's something in the timing...
On MinGW with -O3 results are much more extreme:
5 1 2 ** Running time: 985 micros
1 2 5 ** Running time: 18 micros
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <random>
#include <type_traits>
#include <chrono>
#include <ctime>
#include <string>

std::vector< std::uint_fast32_t > make_test_data( std::size_t n )
{
    static std::mt19937 rng( std::random_device{}() ) ;

    std::vector< std::uint_fast32_t > data;
    while( data.size() < n ) data.push_back( rng() ) ;
    return data ;
}

struct timer
{
    using clock_type = std::conditional< std::chrono::high_resolution_clock::is_steady,
                                         std::chrono::high_resolution_clock, std::chrono::steady_clock>::type ;


    explicit timer( std::string label = "" ) noexcept : label( std::move(label) ) {}
    ~timer()
    {
        const auto end_pc = std::clock() ;
        const auto end_wc = clock_type::now() ;
        const auto processor_millisecs = ( end_pc - start_pc ) * 1000.0 / CLOCKS_PER_SEC ;
        using namespace std::chrono ;
        const auto wall_clock_millisecs = duration_cast<milliseconds>( end_wc - start_wc ).count() ;
        std::cout << label << " : " << processor_millisecs << " millisecs processor, "
                  << wall_clock_millisecs << " millisecs wall clock\n" ;
    }

    const clock_type::time_point start_wc = clock_type::now() ;
    const std::clock_t start_pc = std::clock() ;
    const std::string label ;
};

template < typename SET_TYPE > std::vector< std::uint_fast32_t > find_dups( const std::vector< std::uint_fast32_t >& data )
{
    SET_TYPE set ;
    std::vector< std::uint_fast32_t > dups ;
    for( auto v : data ) if( set.insert(v).second == false ) dups.push_back(v) ;
    return dups ;
}

int main()
{
    const std::size_t N = 2'000'000 ;
    const auto data = make_test_data(N) ;

    {
        timer t("using std::set") ;
        const auto dups = find_dups< std::set<std::uint_fast32_t> >(data) ;
        std::cout << "\nfound " << dups.size() << " duplicate items\n" ;
    }

    {
        timer t("using std::unordered_set") ;
        const auto dups = find_dups< std::unordered_set<std::uint_fast32_t> >(data) ;
        std::cout << "\nfound " << dups.size() << " duplicate items\n" ;
    }
}

On Coliru:
g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
echo ================================
clang++ -std=c++2a -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out

found 471 duplicate items
using std::set : 2701.25 millisecs processor, 2701 millisecs wall clock

found 471 duplicate items
using std::unordered_set : 1249.6 millisecs processor, 1249 millisecs wall clock
================================

found 469 duplicate items
using std::set : 2578.7 millisecs processor, 2578 millisecs wall clock

found 469 duplicate items
using std::unordered_set : 1223.97 millisecs processor, 1223 millisecs wall clock

http://coliru.stacked-crooked.com/a/9d82db611cf15048

(Note that for unordered set, insertion would be expensive when rehashing is required.)
thanks for expanding on this discussion, I'm learning from your exchanges.


I just started a new post with a similar topic (how to learn/write codes more effectively) here
https://www.cplusplus.com/forum/beginner/281962/
Topic archived. No new replies allowed.