Printint contents of vector<string>

I want to print the contents of the vectors i have created that have strings pushed back to them. I have an initialize function that reads in two strings from a file and pushes them to two vectors respectively. I also have a toString function that is supposed to be used to print the vectors. How can i implement this toString function to test my results?

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
  void EditDistance::initialize(Scanner& scanner)
{
  string line1; //strings for input lines
  string line2;
  string scanword1;
  string scanword2;
  //this->length = number of words in sentence1 and sentence2
  ScanLine reader; //ScanLine variables
  ScanLine reader2;

  sentence1.push_back(DUMMYSTRING);  //push back initial
  sentence2.push_back(DUMMYSTRING);  //DUMMYSTRINGs

  line1 = scanner.nextLine();
  reader.openString(line1);
  while(reader.hasNext())
  {
    scanword1 = reader.next();
  }

  line2 = scanner.nextLine();
  reader2.openString(line2);
  while(reader2.hasNext())
  {
    scanword2 = reader2.next();
  }
   if(sentence1.size() != sentence2.size())
  {
    if(sentence1.size() < sentence2.size())
    {
      while(sentence1.size() != sentence2.size())
      {
        sentence1.push_back(DUMMYSTRING);
      }
    }

    if(sentence1.size() > sentence2.size())
    {
      while(sentence2.size() != sentence1.size())
      {
        sentence2.push_back(DUMMYSTRING);
      }
    }
  }

  this->length = sentence1.size();  //length of sentence1 = sentence2 

  for(int i = 0; i < this->length; i++)
  {
    sentence1.push_back(scanword1);
  }

  for(int i = 0; i < this->length; i++)
  {
    sentence2.push_back(scanword2);
  }
}
  string EditDistance::toString()
{
}
Something like cout<<sentence1.at(0) ?
I tried some things along those lines but that only throws a few page full of errors
Show us your first 5 or 10 error, is it compiled before you put cout<<vector.at() ?
Last edited on
Oops i left out a character but this is what is returned after i try to compile:
1
2
3
4
EditDistance.cpp: In member function ‘std::string EditDistance::toString()’:
EditDistance.cpp:110:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
Its just a warning that tell your toString() doesn't return anything
How do i get it to return the contents of the vectors?
that toString function ._. Is it return string or a vector or a string inside vector ?
The only thing i have put in the toString function is cout<<sentence1.at(1)
There is no parameters for toString but i would assume it needs to return a string inside a vector
I don't think it should return something just change the function type to void and that warnig will go away (hopefully)
Is there any other error ? Is it compiled ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <string>

std::string to_string( const std::vector<std::string>& seq, std::string tag = "no name" )
{
    std::string result = "contents of vector '" + tag + "'\n--------------------\n" ;
    for( std::size_t i = 0 ; i < seq.size() ; ++i )
        result += '[' + std::to_string(i) + "] '" + seq[i] + "'\n" ;
    return result ;
}

#define TO_STRING(a) to_string( a, #a )

int main()
{
    std::vector<std::string> my_vector = { "How do i get it",  "to return the contents", "of the vectors?" } ;
    const std::string str = TO_STRING(my_vector) ;
    std::cout << str ;
}

http://coliru.stacked-crooked.com/a/7c5f8a2d9e6e7229
I would like to use the functions as they are given to me because i am new to c++ and don't want to go off too far on my own. The goal of this program is to create an edit distance algorithm but i keep getting stuck. The next problem is to implement a 2d vector using something like:
1
2
3
4
5
6
7
 for(i = 0; i < this->length; i++)
  {
    for(j = 0; j < this->length; j++)
    {
      dist[i][j].push_back(___);
    }
  }


I was told it would be easier to initialize all the contents of the 2d vector to -1 but whenever i try that i get these errors:

1
2
3
4
5
6
7
EditDistance.cpp: In member function ‘void EditDistance::initialize(Scanner&)’:
EditDistance.cpp:80:7: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
   for(i = 0; i < this->length; i++)
       ^
EditDistance.cpp:80:7: note: (if you use ‘-fpermissive’ G++ will accept your code)
EditDistance.cpp:82:9: error: ‘j’ was not declared in this scope
     for(j = 0; j < this->length; j++)
I'd also like to note that the strings that we use as input for the vectors may be different lengths but we push back a dummy string of "XX" to the beginning of each string in the vector and push back the same dummy string to the end of each sentence until they are the same length of words.
Ex:
this is the first line
this is the second and final line

So the desired strings would be "XX this is the first line XX XX" and "XX this is the second and final line"
Topic archived. No new replies allowed.